How to check product expiration duration of any product using php (2 proven function)

with this simple php trick you can check expiration date duration span of any product.

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 insecured

How about using an expired version of an application it will bug your system device

Hey! friend stay upto date on everything

Be versatile that the keep quote

Related

How TO create a Login Form (w3schools vs bootstrap)

Every transactional website should have a login form that...

Best PHP checkdate alternative: How to do date validation in php (exclusive)

this checkdate alternative special function validates date in such...

How to validate date of birth today & get the age from the birthday using php

we have already explained how you can get age...

How to use php to check age for birthday messages

We going to check age for birthday messages using...

the 2 easy ways to create dynamic form inputs in php

these are 2 ways you can create dynamic form...