【发布时间】:2011-07-02 07:15:57
【问题描述】:
如何最好地检查以下日期(格式 dd/mm/yyyy)是否在未来:01/03/2011
澄清一下,这是 2011 年 3 月 1 日。
谢谢。
编辑:时区是通过 date_default_timezone_set('Europe/London'); 设置的;
【问题讨论】:
-
您想如何处理这一天?整个 24 小时周期是否应该相等?
标签: php
如何最好地检查以下日期(格式 dd/mm/yyyy)是否在未来:01/03/2011
澄清一下,这是 2011 年 3 月 1 日。
谢谢。
编辑:时区是通过 date_default_timezone_set('Europe/London'); 设置的;
【问题讨论】:
标签: php
您可以使用 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."
}
【讨论】:
$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";
}
【讨论】: