Search code examples
phpmysqldate-comparison

how do I compare date and display record only if its in todays and before expiry


I want to display database records only if testfrom date is todays and testto date is not over.

The following code I tried below does not seem to be working well, could someone have a look what am I doing wrong?

the stored date value in the database is

testfrom - 2017-02-23 00:00:00
testto - 2017-02-24 23:59:59

my code

    $dt = date("Y-m-d");
    $srt = date("Y-m-d", strtotime($r['testfrom']));
    $end = date("Y-m-d", strtotime($r['testto']));
 if($srt >= $dt || $end <= $dt){

    }

is this the right way?

Appreciate your valuable time


Solution

  • change your condition like this:

     $dt = date("Y-m-d");
        $srt = date("Y-m-d", strtotime("2017-02-23 00:00:00"));
        $end = date("Y-m-d", strtotime("2017-02-24 23:59:59"));
     if($srt == $dt && $end >= $dt){
      echo "yes";
        }