PHP:检查是否EU日期格式是在未来

问题描述:

哪有我,如果以下日期(格式DD/MM/YYYY)是未来最好检查一下:01/03/2011PHP:检查是否EU日期格式是在未来

为了澄清,这是1 2011年3月。

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

+0

你想怎么处理目前的一天吗?整个24小时的时间应该相等吗? – Mikel 2011-02-22 20:50:25

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

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

日期/ d/y或DMY格式 是通过查看各种 部件之间的 隔板消除歧义:如果分离器是 斜杠(/),则美国米/ d /假设y为 ;而如果分隔符是 短划线( - )或点(。),则假定欧洲d-m-y格式为 。

所以把他们放在一起:

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

如果是01/03/2011的12:00,该怎么办?这段代码会说01/03/2011是过去的。 – Mikel 2011-02-22 20:51:01

+0

@Mikel在一分钟内再次运行代码! – AndrewKS 2011-02-22 21:00:33

$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"; 
}