PHP date comparison

后端 未结 9 524
长情又很酷
长情又很酷 2021-02-02 16:10

How would I check if a date in the format \"2008-02-16 12:59:57\" is less than 24 hours ago?

相关标签:
9条回答
  • 2021-02-02 16:20

    e.g. via strtotime and time().
    The difference must be less then 86400 (seconds per day).

    <?php
    echo 'now: ', date('Y-m-d H:i:s'), "\n";
    foreach( array('2008-02-16 12:59:57', '2009-12-02 13:00:00', '2009-12-02 20:00:00') as $input ) {
      $diff = time()-strtotime($input);
      echo $input, ' ', $diff, " ", $diff < 86400 ? '+':'-', "\n";
    }
    

    prints

    now: 2009-12-03 18:02:29
    2008-02-16 12:59:57 56696552 -
    2009-12-02 13:00:00 104549 -
    2009-12-02 20:00:00 79349 +
    

    only the last test date/time lays less than 24 hours in the past.

    0 讨论(0)
  • 2021-02-02 16:21

    There should be you variable date Like

    $date_value = "2013-09-12";
    $Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();
    

    You can Compare both date after convert date into time-stamp so :

    if(strtotime($current_date) >= strtotime($date_value)) {
     echo "current date is bigger then my date value";
    }
    

    OR

    if($current_date_time_stamp >= strtotime($date_value)) {
     echo "current date is bigger then my date value";
    }
    
    0 讨论(0)
  • 2021-02-02 16:25
    if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
      // less than 24 hours ago
    }
    
    0 讨论(0)
  • 2021-02-02 16:26

    Php has a comparison function between two date/time objects, but I don't really like it very much. It can be imprecise.

    What I do is use strtotime() to make a unix timestamp out of the date object, then compare it with the output of time().

    0 讨论(0)
  • 2021-02-02 16:29

    Maybe it will be more easy to understand...

    $my_date        = '2008-02-16 12:59:57';
    $one_day_after  = date('Y-m-d H:i:s', strtotime('2008-02-16 12:59:57 +1 days'));
    
    if($my_date < $one_day_after) {
    echo $my_date . " is less than 24 hours ago!";
    } else {
    echo $my_date . " is more than 24 hours ago!";
    }
    
    0 讨论(0)
  • 2021-02-02 16:39

    Just use it.....

    if(strtotime($date_start) >= strtotime($currentDate))
    {
    // Your code
    }
    
    0 讨论(0)
提交回复
热议问题