PHP - 确定日期是否在将来使用DateTime对象 [英] PHP - Determine if the date is in the future using DateTime Objects

查看:85
本文介绍了PHP - 确定日期是否在将来使用DateTime对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定日期是否在将来,或者不使用 DateTime 对象,但总是回到正面:

I am trying to determine whether a date is in the future or not using DateTime objects but it always comes back positive:

$opening_date = new DateTime($current_store['openingdate']);
$current_date = new DateTime();
$diff = $opening_date->diff($current_date);
echo $diff->format('%R'); // +

if($diff->format('%R') == '+' && $current_store['openingdate'] != '0000-00-00' && $current_store['openingdate'] !== NULL) {
    echo '<img id="openingsoon" src="/img/misc/openingsoon.jpg" alt="OPENING SOON" />';
}

问题是它总是积极的,所以图像显示何时不应该

The problem is it's always positive so the image shows when it shouldn't be.

我一定是在做一些愚蠢的事情,但是它是什么驱使我疯狂!

I must be doing something stupid, but what is it, it's driving me insane!

推荐答案

比你想象的容易得多您可以与 DateTime 对象进行比较:

It's easier than you think. You can do comparisons with DateTime objects:

$opening_date = new DateTime($current_store['openingdate']);
$current_date = new DateTime();

if ($opening_date > $current_date)
{
  // not open yet!
}

这篇关于PHP - 确定日期是否在将来使用DateTime对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆