Domanda

I'm trying to use DateTime to check if a credit card expiry date has expired but I'm a bit lost.

I only want to compare the mm/yy date.

Here is my code so far

$expmonth = $_POST['expMonth']; //e.g 08
$expyear = $_POST['expYear']; //e.g 15

$rawExpiry = $expmonth . $expyear;

$expiryDateTime = \DateTime::createFromFormat('my', $rawExpiry);
$expiryDate = $expiryDateTime->format('m y');

$currentDateTime = new \DateTime();
$currentDate = $currentDateTime->format('m y');

if ($expiryDate < $currentDate) {
    echo 'Expired';
} else {
    echo 'Valid';
}

I feel i'm almost there but the if statement is producing incorrect results. Any help would be appreciated.

È stato utile?

Soluzione

It's simpler than you think. The format of the datess you are working with is not important as PHP does the comparison internally.

$expires = \DateTime::createFromFormat('my', $_POST['expMonth'].$_POST['expYear']);
$now     = new \DateTime();

if ($expires < $now) {
    // expired
}

Altri suggerimenti

You can use the DateTime class to generate a DateTime object matching the format of your given date string using the DateTime::createFromFormat() constructor.

The format ('my') would match any date string with the string pattern 'mmyy', e.g. '0620'. Or for dates with 4 digit years use the format 'mY' which will match dates with the following string pattern 'mmyyyy', e.g. '062020'. It's also sensible to specify the timezone using the DateTimeZone class.

$expiryMonth = 06;
$expiryYear = 20;
$timezone = new DateTimeZone('Europe/London');
$expiryTime = \DateTime::createFromFormat('my', $expiryMonth.$expiryYear, $timezone);

See the DateTime::createFromFormat page for more formats.

However - for credit/debit card expiry dates you will also need to take into account the full expiry DATE and TIME - not just the month and year.

DateTime::createFromFormat will by default use todays day of the month (e.g. 17) if it is not specified. This means that a credit card could appear expired when it still has several days to go. If a card expires 06/20 (i.e. June 2020) then it actually stops working at 00:00:00 on 1st July 2020. The modify method fixes this. E.g.

$expiryTime = \DateTime::createFromFormat('my', $expiryMonth.$expiryYear, $timezone)->modify('+1 month first day of midnight');

The string '+1 month first day of midnight' does three things.

  • '+1 month' - add one month.
  • 'first day of' - switch to the first day of the month
  • 'midnight' - change the time to 00:00:00

The modify method is really useful for many date manipulations!

So to answer the op, this is what you need — with a slight adjustment to format to cater for single digit months:

$expiryMonth = 6;
$expiryYear = 20;
$timezone = new DateTimeZone('Europe/London');
$expiryTime = \DateTime::createFromFormat(
  'm-y',
   $expiryMonth.'-'.$expiryYear,
   $timezone
)->modify('+1 month first day of midnight');

$currentTime = new \DateTime('now', $timezone);

if ($expiryTime < $currentTime) {
    // Card has expired.
}

An addition to the above answers. Be aware that by default the days will also be in the calculation. For example today is 2019-10-31 and if you run this:

\DateTime::createFromFormat('Ym', '202111');

It will output 2021-12-01, because day 31 does not exist in November and it will add 1 extra day to your DateTime object with a side effect that you will be in the month December instead of the expected November.

My suggestion is always use the day in your code.

For op's question:

$y=15;
$m=05;

if(strtotime( substr(date('Y'), 0, 2)."{$y}-{$m}" ) < strtotime( date("Y-m")  ))
{
  echo 'card is expired';
}

For others with full year:

$y=2015;
$m=5;

if(strtotime("{$y}-{$m}") < strtotime( date("Y-m")  ))
{
  echo 'card is expired';
}

Would it not be simpler to just compare the string "201709" to the current year-month? Creating datetime objects will cost php some effort, I suppose.

if($_POST['expYear']. str_pad($_POST['expMonth'],2,'0', STR_PAD_LEFT ) < date('Ym')) {
   echo 'expired';

}

edited as Adam states

The best answer is provided by John Conde above. It it does the minimum amount of processing: creates two correct DateTime objects, compares them and that's all it needs.

It could work also as you started but you must format the dates in a way that puts the year first.

Think a bit about it: as dates, 08/15 (August 2015) is after 12/14 (December 2014) but as strings, '08 15' is before '12 14'.

When the year is in front, even as strings the years are compared first and then, only when the years are equal the months are compared:

$expiryDate  = $expiryDateTime->format('y m');
$currentDate = $currentDateTime->format('y m');

if ($expiryDate < $currentDate) {
    echo 'Expired';
} else {
    echo 'Valid';
}

Keep it simple, as the answer above me says except you need to string pad to the left:

isCardExpired($month, $year)
{
    $expires = $year.str_pad($month, 2, '0', STR_PAD_LEFT);
    $now = date('Ym');

    return $expires < $now;
}

No need to add extra PHP load using DateTime

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top