简体   繁体   中英

Generate expiry date in PHP

I'm trying to generate a list of medicine those are going to be expired in next three month. In the database, I have stored expiry date in the format of MM YYYY. I'm new to PHP, can somebody help me with idea how to achieve this goal

Php code

$NintyDays = date('Y-m-d', strtotime('+90 days')); 
$dbDate = '2018-12-20';
 if (strtotime($dbDate) <= strtotime($NintyDays ))
 {
 // some other code 
}

您可以通过 sql 查询来完成:

mysql_query("SELECT * FROM medicine_table m WHERE m.expiry < (NOW() + INTERVAL 3 MONTH)");

I also advise MySQL solution as above if it can be used.

if you need PHP solution it could be like this:

$expDate = new DateTime('2018-12-20');
$threeMonths = new DateTime();
$threeMonths->add(new DateInterval('P3M'));

if ($expDate < $threeMonths) {
   // code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM