开发者

PHP date comparison

How would I check if a date in the format "200开发者_StackOverflow社区8-02-16 12:59:57" is less than 24 hours ago?


if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }


Just adding another answer, using strtotime's relative dates:

$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
    // Do something
}

I think this makes the code much more readable.


if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
  // less than 24 hours ago
}


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.


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().


Just use it.....

if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}


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!";
}


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";
}


You can use Simple PHP to do this:

$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜