Wednesday, 25 May 2016

Getting differences between dates quickly in PHP or MySQL



Using PHP:
<?php
$expireDate = "2006-02-07";
$year = substr($expireDate, 0, 4);
 $month = substr($expireDate, 5, 2);
$day = substr($expireDate, 8, 2);
$splitExpireDate = (mktime(0, 0, 0, $month, $day, $year));
$today = (mktime(0, 0, 0, date("m"), date("d"), date("Y")));
 $difference = (($today) - ($splitExpireDate));
$convertToDays = ($difference/86400); echo $convertToDays;
?>
Using MySQL:
SELECT (TO_DAYS(expire_date) - TO_DAYS(CURDATE())) AS days_expired FROM tablename;

0 comments:

Post a Comment