【问题标题】:PHP: check if EU date format is in futurePHP:检查欧盟日期格式是否在未来
【发布时间】:2011-07-02 07:15:57
【问题描述】:

如何最好地检查以下日期(格式 dd/mm/yyyy)是否在未来:01/03/2011

澄清一下,这是 2011 年 3 月 1 日。

谢谢。

编辑:时区是通过 date_default_timezone_set('Europe/London'); 设置的;

【问题讨论】:

  • 您想如何处理这一天?整个 24 小时周期是否应该相等?

标签: php


【解决方案1】:

您可以使用 strtotime,并与当前日期进行比较。

首先,您需要将 / 更改为 - 以将其解释为欧洲日期。

m/d/y 或 d-m-y 格式的日期 通过查看来消除歧义 各种分隔符 组件:如果分隔符是 斜线 (/),则美式 m/d/y 为 假定;而如果分隔符是 破折号 (-) 或点 (.),然后 假定为欧洲 d-m-y 格式。

所以把它们放在一起:

$time = strtotime(str_replace("/","-",$date) )
if ($time > time())
{
  echo "$date is in the future."
}

【讨论】:

  • 如果 2011 年 1 月 3 日中午 12:00 怎么办。这段代码会说 01/03/2011 已经过去了。
【解决方案2】:
$date = "01/03/2011";

// convert the date to a time structure
$tmarr = strptime($date, "%d/%M/%Y");

// convert the time structure to a time stamp representing the start of the day
$then = mktime(0, 0, 0, $tmarr['tm_mon']+1, $tmarr['tm_mday'], $tmarr['tm_year']+1900);

// get the current time
$today = mktime(0, 0, 0);

// compare against the current date
if ($then > $today) {
    echo "$date is in the future";
}
elseif ($then == $today) {
    echo "$date is today";
}
else {
    echo "$date is not in the future";
}

【讨论】:

    猜你喜欢
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多