date_default_timezone_set('Europe/London');
$date = '06-17-2013';
if (strtotime("now") > strtotime("+5 days", strtotime($date))) {
echo '5 days have gone by';
} else {
echo 'not yet';
}
This function always returns '5 days have gone by' . I could never get it to return 'not yet' Why?
What you need to use is $date = '17-06-2013';
(DD-MM-YYYY)
or $date = '2013-06-17';
(YYYY-MM-DD)
and not
$date = '06-17-2013';
Try specifying your date by YYYY-MM-DD
. strtotime
doesn't seem to support the MM-DD-YYYY
notation.
You should consider using the SPL DateTime object.
Try this:
$date = new DateTime('2013-06-17');
$date->add(new DateInterval("P5D");
$now = new DateTime();
if($now > $date) {
echo "5 Days have passed";
}
else{
echo "not yet";
}
If you change '06-17-2013'
to this '06/17/2013'
it will work as expected
<?php
date_default_timezone_set('Europe/London');
$date = '06/17/2013';
if (strtotime("now") > strtotime("+5 days", strtotime($date))) {
echo '5 days have gone by';
} else {
echo 'not yet';
}
?>
There is a difference between using forward slash / and hyphen - in the strtotime function.
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.