php validates Payment card expiring date


payment card expiring date format is always MM/YYYY [2digit/ 4digit] i.e 04/2030
payment card normally expires 3-4 years after issuance from the bank.

In the code below we use the following functions

  • php strlen check if the date parameters that’s MM is 2 digits and YYYY is 4 digits
  • php is_numeric verifies if the date parameters are all numbers
  • php in_array checks if the month and year is within the specified range of expiration span like 5year in our example code below
  • php cal_days_in_month() is used to calculate the month and year
  • when the payment expiration year and month matches the user current year and month then the system will tell the user the interval of days remain for the card to expire
<form method="post">
<label for="expdate">Expiring date</label>
<input type="text" name="expdate" placeholder="MM/YYYY">
<input type="submit" value="check" name="check">
<br><span style="font-size:x-small;">date format: <span style="color:red; font-weight:bold;">MM/YYYY</span></span>


$date = $_POST["expdate"];

$date = explode("/", $date); // date separator is '/' slash.
$expiration = $date[1]-idate('Y');
$expiration = "";

if(count($date)<2 or count($date)>2 ){//check if date format is given properly
	echo 'date parameters must be according to format';
}elseif(strlen($date[0])<2 or strlen($date[1])<4 or strlen($date[0])>2 or strlen($date[1])>4){//check if mm is 2 digits only, yyyy is 4 digits ONLY
	echo 'date parameters must be according to format';
}elseif(!is_numeric($date[0]) or !is_numeric($date[1])){//check if mm and yyyy numbers ONLY
	echo 'all date parameters must numeric';
}elseif(!in_array($date[0],range(01,30))){ //validate the month
	echo 'the given date MONTH is invalid';

if(!in_array($expiration, range(1,5))){// validate to ensure the payment is still valid between 1 to 5 years range
	if($date[1] == idate('Y') && intval($date[0]) == intval(idate('m'))){
		$days = cal_days_in_month(CAL_GREGORIAN, idate('m'), idate('Y'));
		echo "the payment card date is still valid <br>it will expire in <b>".(intval($days)-intval(date('d')))." days</b> from now";
	}elseif($date[1] == idate('Y') && intval($date[0]) < intval(idate('m'))){ //validate if the current year is valid but the month has expired
		echo 'the payment card MONTH is expired you cannot use this card for payment';
	}elseif($date[1] == idate('Y') && intval($date[0]) > intval(idate('m'))){
		$period = date_create_from_format('m-Y', $date[0].'-'.$date[1]);
		echo "the payment card date is still valid <br>it will expire on <b>".(date_format($period, 'M'))." this year</b>";
		echo 'your payment card have expired or invalid out of expiration duration or archived from payment card database';
	}else{ // validate to ensure the payment is still valid between 1 to 5 years range	
		echo 'your payment card is still valid and will expire in - '.(idate("Y")+$expiration);


Owner and founder of He is a passionate programmer with enthusiasm in daily discovering of solving logic with new block of codes. He loves creating contents and blogging his new discoveries on programming language and related peripherals.

Share this article

Recent posts

how php processes Form data

your first php-enabled page

php file and document

installation of php

Google search engine

Popular categories


  1. My brother recommended I might like this website.
    He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information!

  2. I think everything wrote was actually very logical. However, consider this, what if you
    added a little information? I am not saying your information isn’t solid., but suppose you added a title that
    makes people desire more? I mean php validates Payment card
    expiring date | shopinson is kinda vanilla. You ought to glance
    at Yahoo’s home page and note how they create article titles to get people to click.
    You might add a video or a picture or two to get people interested about everything’ve written.
    Just my opinion, it would bring your posts a little livelier.


Please enter your comment!
Please enter your name here