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?

conmen
  • 2,377
  • 18
  • 68
  • 98

5 Answers5

18

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.

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
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.

Community
  • 1
  • 1
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
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';
  }
?>
Jijo
  • 21
  • 2
  • The example above should work ,apparently strtotime does not work with d/m/Y date format just FYI, – Jijo Feb 07 '17 at 19:26
0

You can do it in mysql itself like below:

select if(cur_date()>expire_date,'expire','active') as status from tableName
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
-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';
}
GautamD31
  • 28,552
  • 10
  • 64
  • 85