简体   繁体   English

PHP:检查将来是否使用欧盟日期格式

[英]PHP: check if EU date format is in future

How can I best check if the following date (format dd/mm/yyyy) is in the future: 01/03/2011 我怎样才能最好地检查以下日期(格式为dd / mm / yyyy)是否是将来的日期:01/03/2011

To clarify, this is 1 March 2011. 为了澄清,这是2011年3月1日。

Thanks. 谢谢。

EDIT: timezone is set via date_default_timezone_set('Europe/London'); 编辑:通过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";
}

You can use strtotime, and compare against the current date. 您可以使用strtotime,并与当前日期进行比较。

First you need to change the / to a - for it to be interpreted as an European date. 首先,您需要将/更改为-才能将其解释为欧洲日期。

Dates in the m/d/y or dmy formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; 通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果分隔符为斜杠(/),则假定为美国m / d / y; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed. 相反,如果分隔符是破折号(-)或点(。),则采用欧洲dmy格式。

So putting it all together: 所以放在一起:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM