[英]PHP: check if EU date format is in future
我怎样才能最好地检查以下日期(格式为dd / mm / yyyy)是否是将来的日期:01/03/2011
为了澄清,这是2011年3月1日。
谢谢。
编辑:通过date_default_timezone_set('Europe / London');设置时区;
$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";
}
您可以使用strtotime,并与当前日期进行比较。
首先,您需要将/更改为-才能将其解释为欧洲日期。
通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果分隔符为斜杠(/),则假定为美国m / d / y; 相反,如果分隔符是破折号(-)或点(。),则采用欧洲dmy格式。
所以放在一起:
$time = strtotime(str_replace("/","-",$date) )
if ($time > time())
{
echo "$date is in the future."
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.