PHP日期比较
我如何检查格式为“2008-02-16 12:59:57”的日期是否少于24小时前?例如via和time()。PHP日期比较,php,date,time,Php,Date,Time,我如何检查格式为“2008-02-16 12:59:57”的日期是否少于24小时前?例如via和time()。 差值必须小于86400(每天秒) 过去只有最后一次测试日期/时间少于24小时。if((time()-strotime(“2008-02-16 12:59:57”)
差值必须小于86400(每天秒) 过去只有最后一次测试日期/时间少于24小时。
if((time()-strotime(“2008-02-16 12:59:57”)<24*60*60){
if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
// less than 24 hours ago
}
//不到24小时前
}
Php有一个新的功能,但我不是很喜欢它。它可能不精确
我要做的是使用strotime()使unix时间戳超出日期对象,然后将其与time()的输出进行比较 使用strotime的相对日期添加另一个答案:
$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
// Do something
}
$date='2008-02-16 12:59:57';
如果(strotime($date+1天))只需使用它
if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}
应该有你喜欢的可变日期
$date_value = "2013-09-12";
$Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();
您可以在将日期转换为时间戳之后比较这两个日期,以便:
if(strtotime($current_date) >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
或
也许这会更容易理解
$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!";
}
$my_date='2008-02-16 12:59:57';
$one_day_after=日期('Y-m-d H:i:s',标准时间('2008-02-16 12:59:57+1天));
如果($my_date<$one_day_after){
echo$my_date.“不到24小时前!”;
}否则{
echo$my_date.“已经超过24小时了!”;
}
您可以使用简单的PHP来实现这一点:
$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();
DateTime
仅为PHP5.3及以上版本。如果他需要“不到24小时”,我想这是可以的。请记住不要假设一天总是有24小时(如你在回答中所说)。如果将时差改为日差,则最好使用DateTime库。有关时间问题的更好解释,请参阅本文:
if(strtotime($current_date) >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
if($current_date_time_stamp >= strtotime($date_value)) {
echo "current date is bigger then my date value";
}
$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!";
}
$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();