0

Hi i'm trying to make something that compares dates from my database to check if my license is expired.

My dates are stored in this format: 18/05/2019 (d/m/Y)

in my code i create a variable for today's date:

$TodayDate = Date('d/m/Y');

then i get the row that contains the date in my database like so:

$DBLicenseExpireDate  = $row["LicenseExpireDate"];

and finally i try to see if the date is expired:

 if ($TodayDate < $DBLicenseExpireDate) {
           }

i've also tried:

 if ($TodayDate < strtotime($DBLicenseExpireDate)) {
           }

in almost all cases i tried it said it was expired while it was not.

I get some weird results. Example: If today's date is : 05/18/2019 and the expire date is: 06/06/2019 and i try this if statement:

if($todayDate < $expireDate)
{
  echo 'not expired';
}
else
{
echo 'expired';
}

it still results as expired. seen million questions about checking expire dates, tried them all. I must be doing something wrong.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Siy
  • 55
  • 8

2 Answers2

3
  1. Don't use strtotime() for date math. Use `DateTime() which is much better suited for working with dates.
  2. Your date format is not compatible with strtotime(). When using m/d/Y format is is assumed that you are using US date format and dates like 18/05/2019 is evaluated as "the fifth day of the 18th month of 2019". Obviously that is not what you mean and you will get 1970-01-01 as a result.

Use DateTime::CreateFromFormat() to get the date you want and then do your comparison:

// Parse the european date format
$date1 = DateTime::CreateFromFormat('%d/%m/%Y', '18/05/2019');
// Get today ("now")
$date2 = new DateTime(); 
// DateTime objects are comparable so you can compare these two variables directly
if ($date2 < $date1) {
    // today is before the 18th of May
}
else {
    // today is after the 18th of May
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
-1
$TodayDate = Date('d/m/Y');
echo '<br/>'.$TodayDate;

//$DBLicenseExpireDate  = $row["LicenseExpireDate"];
//Suppose $DBLicenseExpireDate is 17/05/2019
$DBLicenseExpireDate = '17/05/2019';
echo '<br/>'.$DBLicenseExpireDate;

$TodayDateDateTimeStamp = DateTime::createFromFormat('!d/m/Y', $TodayDate)->getTimestamp();
$DBLicenseExpireDateDateTimeStamp = DateTime::createFromFormat('!d/m/Y', $DBLicenseExpireDate)->getTimestamp();

echo '<br/>'.$TodayDateDateTimeStamp;
echo '<br/>'.$DBLicenseExpireDateDateTimeStamp;

if($TodayDateDateTimeStamp > $DBLicenseExpireDateDateTimeStamp)
    echo "<br/>License Expired";
else
    echo "<br/>Not Expired";

Reference https://php.net/manual/en/function.strtotime.php

  • I had to downvote you because your answer is the same as mine but you don't bother to explain *why* it solves their problem. I also don't know why you linked to `strtotime()` when it isn't part of your answer. And PHP does not have a built in `Date()` class. – John Conde May 18 '19 at 02:20
  • He's calling the date() function but using an uppercase D which works the same. – ryantxr May 18 '19 at 03:30
  • @JohnConde have you run my code ? Run this and if you are getting any error let me know. Downvote is okay. As you have posted it just few min ago.. doen't mean i have copied your code.. Thanks – Brain Developer May 18 '19 at 08:44