php strtotime does not work
Solution 1
Here is working code
<?php
$exp_date = "22/01/2014";
$exp_date = str_replace('/', '-', $exp_date);
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
echo $expiration_date.' | '.$today.'<br>';
if($expiration_date > $today){
echo 'Still Active';
} else {
echo 'Time Expired';
}
?>
Actually strtotime() does not work with format 'd/m/Y'
hope it helps you.
Solution 2
A note on the strtotime()
manual.
Dates in the
m/d/y
ord-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.To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or
DateTime::createFromFormat()
when possible.
Solution 3
<?php
$exp_date = "22/01/2014";
$exp_date = str_replace('/', '-', $exp_date);
$todays_date = date("d-m-Y");
$today = strtotime($todays_date);
$expiration_date = strtotime($exp_date);
echo $expiration_date.' | '.$today.'<br>';
if($expiration_date > $today){
echo 'Still Active';
} else {
echo 'Time Expired';
}
?>
conmen
Updated on June 13, 2022Comments
-
conmen almost 2 years
I got an issue on below script, assume
$exp_date
is retrieved from db, I want to compare expiry date with today date to check whether membership's is still alive.There is nothing to display but only
Time Expired
, what's wrong with the code?The data retrieved from expiry column in db is set as varchar (which is built by ex-colleague).
$exp_date = "22/01/2014"; $todays_date = date("d/m/Y"); $today = strtotime($todays_date); $expiration_date = strtotime($exp_date); echo $expiration_date.' | '.$today.'<br>'; if($expiration_date > $today){ echo 'Still Active'; } else { echo 'Time Expired'; }
Anyone can help with?
-
Jijo about 7 yearsThe example above should work ,apparently strtotime does not work with d/m/Y date format just FYI,