Payment Card Expiration Date Validation

Payment Card Expiration Date Validation
Payment Card Expiration Date Validation

When building web applications or websites that processes payment one crucial process is validating the payment card expiration date. In this article, we’ll dive into the importance of validating payment card expiration dates and provide a detailed PHP implementation to achieve this. Ensuring the accuracy of payment card data is essential for the smooth operation of financial services.

companies such Mastercard, Discover, Verve and Visa sees it as basic priority to do this validation check.

Validating Payment Card Expiration Date

Before we proceed, it’s important to understand that payment card expiration dates typically follow the format “mm/yyyy,” where “mm” represents the month, and “yyyy” represents the year.

Payment card validation Code 1

the code below will split the given date into month and year

$expdate = '8/2024'; //payment card expiration date
$expdate = explode('/', $expdate);
$exp_year = $expdate[1];
$exp_month = $expdate[0];

Now let's use php range function to check if the month is within 12 months in the year

$cardmonth = range(1,12);
if(in_array($exp_month, $cardmonth)){
	echo 'Awesome -Valid card expiration month';
	echo 'Hey! -Invalid card expiration month [or] Expired';


I hope you got same response “Awesome -Valid card expiration month”

I think this should be simple and easy by comparison as we have done in our earlier post (date of birth confirmation (+validation) in php)

this kind of validation requires no checkdate php we’ve to program it all in our logic

we are going to programmatically compare the given expiring date of the payment card and current date

let’s keep going, it time to validate the year of expiration for the payment if it is with in the acceptable expiration year
lets do a bit exact we did in formless: An advanced date inputs without bootstrap form control (just dropdowns)

Payment card validation Code 2

$cardyear = idate('Y');
$cardyearend = $cardyear - 0;
$cardyearstart = $cardyearend + 3;
$cardyears = range($cardyearstart,$cardyearend);

Since payment card life span is normally three years 3yrs
we subtracted 0 from the current year to mark starting range of the acceptable year and also added 3 to the current year to mark the end of the range

Now let’s validate year for the payment card expiration date

Payment card validation Code 3

if(in_array($exp_year, $cardyears)){
	echo 'Awesome -Valid card expiration Year';
	echo 'Hey! -Invalid card expiration Year [or] Expired';

Well you should be getting awesome as the result when you run the application else you are not following me properly

Oh before I forgot you can manipulate the current year to desired $cardyear as you wish
depending on your objectives.

Put together the logic code should be combine like this below

Payment card validation Code 4

if(in_array($exp_year, $cardyears) && in_array($exp_month, $cardmonth)){
	echo 'the year and month of the card expiration is valid'; //do what you like here
	echo 'invalid or expired payment card date ';

Now let’s check if the card expiration have exhausted
Though you may validate the card expiration date like this straight see code below using php checkdate function
And that exact what we should be doing easy and simple logically advanced

well it looks like we are going to do what we’ve done in previous post about validating date of birth and wishing user happy birthday

But we are going to tweak the code a little bit different

Payment card validation Code 5

$origin = date_create($exp_year.'-'.$exp_month.'-'.cal_days_in_month(CAL_GREGORIAN, $exp_month, $exp_year));
$target = date_create(date('Y-m-d'));
$interval = date_diff($target, $origin);
$interval = intval($interval->format('%R%a'));
//since it a bool we are using logic to output it result

if($interval > -1){
	echo '>>>>>> The payment card have not expired, it is still valid';
	echo 'xxxxxx The payment card have expired/due or invalid';

Awesome it is, isn’t it we use php cal_days_in_month(CAL_GREGORIAN), date_create and date_diff function to check if the card is due to expire or not.


from you will see that number of days in a given month is provided using cal_days_in_month(int $calendar, int $month, int $year).

we used this because we considered the expiration day of any payment card is the last day in the month specified on the card

You got it now every payment card have expiration day but it is not written and you also know now that it should be the last day of it specified month that card.

payment card

I have come to realize that payment cards such as mastercard, discover cards and verve card or visa card expiration date is normally due after the given date not before or on same date

therefore we should consider the observation while preparing the logic to validate expiration date and also confirm if a card have been expired

Look closely in the logic you will see -1 this depict that a day after the expiration it will report expired message.

Aw! interesting the result is nice
the payment card have not expired! this the response I got from mine

let us now change our local machine to exceed 3years from your current year or you can alter the given date in this project to expire atleast a month or more from your current year
if you got payment card have expired then you are doing well

Wait before you exit remember to reset your computer to updated correct date if you don’t your internet connection may start experiencing issues connecting


Please enter your comment!
Please enter your name here