any product expiration check with this new php code

php check product expiration duration

0
392
any product expiration check with this new php code
any product expiration check with this new php code

with this simple php code you can now check any product expiration date or duration span. Do you have a medicine or a beverages, You might want to check it expiration date before you take it. it is necessary to check product expiration duration.

Well you wish to check their expiration date using php or as an application designer and developer you wish to ensure in your app such as e-commerce app that products expiration day is still valid. C’mon here’s a proven function for you

The process code to check product expiration duration

just do as we did below, remember we are still using function as predefined in our previous post the Best PHP checkdate() alternative: How to do date validation in php (special function).

and we did a little tweak to suit our need in the current topic.

 
<?php 
function get_expiration_date($date){
	$dateparameters = array();
	$date = preg_replace("/[^0-9\']/", ",", $date);	
	$date = explode(',', $date);
	$exp = '';
	if(count($date) == 3){
    for ($i = 0; $i < 3; ++$i) {
        $dateparameters []= $date[$i];
    }
	
	$year = $dateparameters[0];
	$month = $dateparameters[1];
	$actualmonth = $dateparameters[1];
	$actualday = $dateparameters[2];
	$actualdate = $year.'-'.$month.'-'.$actualday;
	$day = cal_days_in_month(CAL_GREGORIAN, $month, $year);

	$day = range(1, $day); // day range
	$month = range(1, 12); // month range
	
	if(in_array($actualday, $day) && in_array($actualmonth, $month)){ //Check if actual month and day is in range of days that should be in the provided month of the given year
		$origin = date_create($actualdate); // create the date of birth in proper date parameters
		$target = date_create(date('Y-m-d')); // creates the current day from the user is calculating from
		$exp = date_diff($target, $origin);
		$exp = intval($exp->format('%R%a'))+1;
	}
		
	}
return $exp;
}
?>

There is a +1 in the above function since we considered that a product be it beverage or drug does not actually expire on or before the expiration date but immediately after the day specified as the expiration date. No let’s include the functions output in our work. it should look like below php logic code.

the final code

 <?php
$exp_date = '2022/9/21';

if(get_expiration_date($exp_date) > 0){
	echo '>>>>>> The product expiration date is valid and have not expired it is still good till <b>'.get_expiration_date($exp_date).'</b> day(s) ahead';
}else{
	echo 'xxxxxx The product expiration date is invalid and have expired, it is no longer good since <b>'.str_replace('-','',get_expiration_date($exp_date)).'</b> day(s) ago';
}
?>

Well it is important you don’t consume an outdated expired drugs, food and beverages for your health security. Same thing apply to consuming an expired information make your iq level mentally insecure.

How about using an expired version of an application it will bug your system device. Hey! friend stay upto date on everything Be versatile.

LEAVE A REPLY

Please enter your comment!
Please enter your name here