Compare given date with today

前端 未结 13 1995
别那么骄傲
别那么骄傲 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.

    0 讨论(0)
  • 2020-11-22 16:43
    $toBeComparedDate = '2014-08-12';
    $today = (new DateTime())->format('Y-m-d'); //use format whatever you are using
    $expiry = (new DateTime($toBeComparedDate))->format('Y-m-d');
    
    var_dump(strtotime($today) > strtotime($expiry)); //false or true
    
    0 讨论(0)
  • 2020-11-22 16:43

    Few years later, I second Bobby Jack's observation that last 24 hrs is not today!!! And I am surprised that the answer was so much upvoted...

    To compare if a certain date is less, equal or greater than another, first you need to turn them "down" to beginning of the day. In other words, make sure that you're talking about same 00:00:00 time in both dates. This can be simply and elegantly done as:

    strtotime("today") <=> strtotime($var)
    

    if $var has the time part on 00:00:00 like the OP specified.

    Replace <=> with whatever you need (or keep it like this in php 7)

    Also, obviously, we're talking about same timezone for both. For list of supported TimeZones

    0 讨论(0)
  • 2020-11-22 16:45

    You can use the DateTime class:

    $past   = new DateTime("2010-01-01 00:00:00");
    $now    = new DateTime();
    $future = new DateTime("2021-01-01 00:00:00");
    

    Comparison operators work*:

    var_dump($past   < $now);         // bool(true)
    var_dump($future < $now);         // bool(false)
    
    var_dump($now == $past);          // bool(false)
    var_dump($now == new DateTime()); // bool(true)
    var_dump($now == $future);        // bool(false)
    
    var_dump($past   > $now);         // bool(false)
    var_dump($future > $now);         // bool(true)
    

    It is also possible to grab the timestamp values from DateTime objects and compare them:

    var_dump($past  ->getTimestamp());                        // int(1262286000)
    var_dump($now   ->getTimestamp());                        // int(1431686228)
    var_dump($future->getTimestamp());                        // int(1577818800)
    var_dump($past  ->getTimestamp() < $now->getTimestamp()); // bool(true)
    var_dump($future->getTimestamp() > $now->getTimestamp()); // bool(true)
    

    * Note that === returns false when comparing two different DateTime objects even when they represent the same date.

    0 讨论(0)
  • 2020-11-22 16:46

    One caution based on my experience, if your purpose only involves date then be careful to include the timestamp. For example, say today is "2016-11-09". Comparison involving timestamp will nullify the logic here. Example,

    //  input
    $var = "2016-11-09 00:00:00.0";
    
    //  check if date is today or in the future
    if ( time() <= strtotime($var) ) 
    {
        //  This seems right, but if it's ONLY date you are after
        //  then the code might treat $var as past depending on
        //  the time.
    }
    

    The code above seems right, but if it's ONLY the date you want to compare, then, the above code is not the right logic. Why? Because, time() and strtotime() will provide include timestamp. That is, even though both dates fall on the same day, but difference in time will matter. Consider the example below:

    //  plain date string
    $input = "2016-11-09";
    

    Because the input is plain date string, using strtotime() on $input will assume that it's the midnight of 2016-11-09. So, running time() anytime after midnight will always treat $input as past, even though they are on the same day.

    To fix this, you can simply code, like this:

    if (date("Y-m-d") <= $input)
    {
        echo "Input date is equal to or greater than today.";
    }
    
    0 讨论(0)
  • 2020-11-22 16:47
    strtotime($var);
    

    Turns it into a time value

    time() - strtotime($var);
    

    Gives you the seconds since $var

    if((time()-(60*60*24)) < strtotime($var))
    

    Will check if $var has been within the last day.

    0 讨论(0)
提交回复
热议问题