CodeIgniter Forums
Comparison Operators >= in Control Structures why false? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Comparison Operators >= in Control Structures why false? (/showthread.php?tid=73280)



Comparison Operators >= in Control Structures why false? - DELE - 04-07-2019

What am I doing right?

PHP Code:
$date  '2019-04-07';

$date1 '2019-04-01';
$date2 '2019-04-15';
$date3 '2019-04-16';
$date4 '2019-04-30';

 
       if ($date >= $date1 && $date <= $date2) {
     
       return $this->db->query("
                SELECT *
                FROM incomes
                WHERE income_campaign = '00001'
                AND income_date BETWEEN '
$date3' AND '$date4'
                "
)->result_array();
 
       } else {
     
       return $this->db->query("
                SELECT *
                FROM incomes
                WHERE income_campaign = '00001'
                AND income_date BETWEEN '
$date1' AND '$date2'
                "
)->result_array();
 
       


My question :

PHP Code:
$date  '2019-04-07';

$date1 '2019-04-01';
$date2 '2019-04-15';
$date3 '2019-04-16';
$date4 '2019-04-30';

 
       if ($date >= $date1 && $date <= $date2) { // FALSE VALUE WHICH SHOULD BE TRUE
     
   CODE // CODE ABOVE SHOULD SHOULD EXECUTE THIS CODE
 
       } else {
     
   CODE // BUT THE CODE ABOUT EXECUTING THIS CODE
 
       }

// $date >= $date1 && $date <= $date2
// I think this produces true value but why does it produce false values?
// How to produce true value? 



RE: Comparison Operators >= in Control Structures why false? - jreklund - 04-07-2019

It's true according to my tests.
http://sandbox.onlinephpfunctions.com/code/9674644a817abe76ad0741940e3eaa1905fc9610


RE: Comparison Operators &gt;= in Control Structures why false? - Paradinight - 04-07-2019

PHP is a funny language.

https://www.php.net/manual/en/language.operators.comparison.php

Use https://www.php.net/manual/en/datetime.diff.php


RE: Comparison Operators >= in Control Structures why false? - skunkbad - 04-07-2019

PHP has no way of knowing that your dates are dates. There are a number of ways you could handle it, but the simplest would be to wrap your dates in strtotime()


PHP Code:
$date  '2019-04-07';

$date1 '2019-04-01';
$date2 '2019-04-15';
$date3 '2019-04-16';
$date4 '2019-04-30';

if( 
    
strtotime($date) >= strtotime($date1) && 
    
strtotime($date) <= strtotime($date2
){
    
//...
} else {
    
//...




RE: Comparison Operators &gt;= in Control Structures why false? - InsiteFX - 04-07-2019

This should clarify it for you.

'AND' vs '&&' as operator in PHP


RE: Comparison Operators >= in Control Structures why false? - DELE - 04-09-2019

(04-07-2019, 08:43 AM)skunkbad Wrote: PHP has no way of knowing that your dates are dates. There are a number of ways you could handle it, but the simplest would be to wrap your dates in strtotime()


PHP Code:
$date  '2019-04-07';

$date1 '2019-04-01';
$date2 '2019-04-15';
$date3 '2019-04-16';
$date4 '2019-04-30';

if( 
    
strtotime($date) >= strtotime($date1) && 
    
strtotime($date) <= strtotime($date2
){
    
//...
} else {
    
//...


SOLVED. THANK YOU FOR HELPING


RE: Comparison Operators >= in Control Structures why false? - MrJunaidShahid - 04-10-2019

(04-07-2019, 08:43 AM)skunkbad Wrote: PHP has no way of knowing that your dates are dates. There are a number of ways you could handle it, but the simplest would be to wrap your dates in strtotime()


PHP Code:
$date  '2019-04-07';

$date1 '2019-04-01';
$date2 '2019-04-15';
$date3 '2019-04-16';
$date4 '2019-04-30';

if( 
    
strtotime($date) >= strtotime($date1) && 
    
strtotime($date) <= strtotime($date2
){
    
//...
} else {
    
//...


You are exactly right, in the top when defining the date it is the string and when we compare any string resultant will be true always. In only case if it is integer or date in right format then we can compare.