I want to fetch data according to date which is expired.
I have database like this
Equipment
Purchase Date
Expiry Date
Laptop
2015-07-15
2016-07-15
Mobile
2012-07-15
2013-07-15
Desktop
2011-07-15
2012-07-15
Now I want to fetch data which is expired, if the purchase date lies under 365 days from current date that means Equipment not expired, else expired. So, How to fetch data data which is expired and How to fetch data data which is not expired.
I managed to count date and print the condition like this.
$purchasedate = strtotime ($row['purchasedate']);
$todayDate = strtotime (date("j-m-Y"));
$timeDiff = abs($todayDate - $supplydate1);
$numberDays = $timeDiff/86400;
$numberDays = intval($numberDays);
if ($numberDays > 365)
{
$expiry = 'Warranty Expired';
} else {
$expiry = 'Under Warranty';
}
But i only want to fetch data which is expired, and which is not expired.
Please help, Thanks.
Use MySQL date arithmetic.
SELECT equipment,
CASE WHEN expiryDate > CURDATE() THEN 'Current'
ELSE 'Expired' END
FROM table
If you want to display only the expired rows, append this WHERE
clause to your query.
WHERE expiryDate <= CURDATE()
There are all sorts of cool things you can do with date arithmetic. For example, to find all the rows expiring between now and two months from now, use this WHERE
clause.
WHERE expiryDate > CURDATE()
AND expiryDate <= CURDATE() + INTERVAL 2 MONTH
Similarly, if you want to see rows where the purchase date is a year ago or more, you can do this.
WHERE purchaseDate <= CURDATE() 1 INTERVAL 1 YEAR
This SQL data arithmetic allows you to escape the need to count days or months; it knows the necessary calendar details.
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.