• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparison Operators >= in Control Structures why false?

#1
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? 
Reply

#2
It's true according to my tests.
http://sandbox.onlinephpfunctions.com/co...1905fc9610
Reply

#3
PHP is a funny language.

https://www.php.net/manual/en/language.o...arison.php

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

#4
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 {
    
//...

Reply

#5
This should clarify it for you.

'AND' vs '&&' as operator in PHP
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(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
Reply

#7
(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.
.NET and CodeIgniter Software Engineer 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.