简体   繁体   中英

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

To clarify, this is 1 March 2011.

Thanks.

EDIT: timezone 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 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; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed.

So putting it all together:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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