开发者

PHP: check if EU date format is in future

开发者 https://www.devze.com 2023-02-13 06:29 出处:网络
How can I best check if the following date (format dd/mm/yyyy) is in the future: 01/03/2011 To clarify, this is 1 March 2011.

How can I best check if the following date (format dd/mm/yyyy) is in the future: 01/03/2011

To clarify, this is 1 March 2011.

Thanks.

EDIT: timezone开发者_Go百科 is set via 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.

First you need to change the / to a - for it to be interpreted as an European date.

Dates in the m/d/y or d-m-y 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; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

So putting it all together:

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

精彩评论

暂无评论...
验证码 换一张
取 消