如何发现今天的日期是否在其他2个日期之间? [英] How to discover if today's date is between 2 other dates?

查看:134
本文介绍了如何发现今天的日期是否在其他2个日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确理解语法,我只需要一个IF语句来检查今天是否在一组日期之间,如果是,则回显。该如何调整?

I can't quite get the syntax right, I just need an IF statement to check if today is between a set of dates and if so, echo. How should this be tweaked?

$now = time('Y/m/d');
$date_september = '2014/09/01';
$date_october = '2014/10/01'; 

if ($now > $date_september && $now < $date_october) 
{
    echo "do this...";
}


推荐答案


  1. 使用 DateTime()。它使日期比较更容易,因为这些对象具有可比性。

  2. 您的如果语句无效。 PHP不支持任何比较的简写语法。

  1. Use DateTime(). It makes date comparisons easier as those objects are comparable.
  2. Your if statement is invalid. PHP doesn't support any shorthand syntax for comparisons.

试试这个;

$now            = new DateTime();
$date_september = new DateTime('2014-09-01');
$date_october   = new DateTime('2014-10-01'); 
if ($now > $date_september && $now < $date_october) {
    echo "do this...";
}

这篇关于如何发现今天的日期是否在其他2个日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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