Reputation: 3035
I have a mysql datetime object, let's call it "purchase date", like so 2014-04-15 14:47:39
.
I also have a mysql time object, let's call it "shelf life", like so 168:00:00
, meaning that product has a shelf life of 168 hours, or seven days, after its purchase date. I would like to know how many days there are until this product expires, with the following formula (or something similar)
daysRemainingBeforeExpiring = [purchase_date (datetime) + shelf_life (time)] - now(datetime)
Please correct me if my formula is wrong, but I believe this should return the number of days before this item expires (or a negative number if it's expired). How can I perform this math operation on the datetime and time objects in PHP?
Upvotes: 0
Views: 102
Reputation: 219794
$purchase_date = new DateTime('2014-04-15 14:47:39');
list($hours, $minutes, $seconds) = explode(':', '168:00:00');
$shelf_life = new DateInterval("PT{$hours}H{$minutes}M{$seconds}S");
$expires_date = $purchase_date->add($shelf_life);
$now = new DateTime();
$diff = $now->diff($expires_date);
echo $diff->format('%r%d Days');
Upvotes: 1