Compare given date with today

前端 未结 13 2170
别那么骄傲
别那么骄傲 2020-11-22 16:22

I have following

$var = \"2010-01-21 00:00:00.0\"

I\'d like to compare this date against today\'s date (i.e. I\'d like to know if this

13条回答
  •  感动是毒
    2020-11-22 16:41

    That format is perfectly appropriate for a standard string comparison e.g.

    if ($date1 > $date2){
      //Action
    }
    

    To get today's date in that format, simply use: date("Y-m-d H:i:s").

    So:

    $today = date("Y-m-d H:i:s");
    $date = "2010-01-21 00:00:00";
    
    if ($date < $today) {}
    

    That's the beauty of that format: it orders nicely. Of course, that may be less efficient, depending on your exact circumstances, but it might also be a whole lot more convenient and lead to more maintainable code - we'd need to know more to truly make that judgement call.

    For the correct timezone, you can use, for example,

    date_default_timezone_set('America/New_York');
    

    Click here to refer to the available PHP Timezones.

提交回复
热议问题