php strtotime does not work
Asked Answered
S

5

6

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?

Scrouge answered 13/3, 2013 at 7:22 Comment(3)
In Db you are storing expiration date as like this..??d/m/YCayman
i suggest u store your time & date using this format "Y-m-d H:i:s". easier to work around on later stage. just my opinionCroteau
Read more about this php.net/manual/en/function.strtotime.phpKowalczyk
O
20

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.

Overmantel answered 13/3, 2013 at 7:29 Comment(0)
N
6

A note on the strtotime() manual.

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.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

Nomenclature answered 13/3, 2013 at 7:25 Comment(0)
S
2
<?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';
  }
?>
Sun answered 7/2, 2017 at 19:25 Comment(1)
The example above should work ,apparently strtotime does not work with d/m/Y date format just FYI,Sun
A
0

You can do it in mysql itself like below:

select if(cur_date()>expire_date,'expire','active') as status from tableName
Abrade answered 13/3, 2013 at 7:26 Comment(0)
C
-1

Try like this

$todays_date=date('d/m/y');
$tempArr=explode('/', "22/01/2014");
$exp_date = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));
$expiration_date = strtotime($exp_date);

echo $expiration_date.' | '.$today.'<br>';

if($expiration_date > $today){ 
    echo 'Still Active';
} else { 
    echo 'Time Expired';
}
Cayman answered 13/3, 2013 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.