Jump to content

date difference, am I doing this right?


tryingtolearn

Recommended Posts

Hi all, can someone tell me if I am going about this the correct way

(Or if there is a better way to do it)

 

I need to get the difference between the time now and the timestamp of a MYSQL timestamp table.

 

so it reads - 3 days 23 hours 14 minutes 32 seconds (For example)

 

I think Its correct but for some reason it is really confusing me and just wondering if I should be doing it different.

 

//current time
$date1 = time();
//$exp is a mySQL timestamp field
$date2 = strtotime($exp);
$dateDiff = $date2 - $date1;
$daysLeft = floor($dateDiff/(60*60*24));
$hoursLeft = floor(($dateDiff-($daysLeft*60*60*24))/(60*60));
$minutesLeft = floor(($dateDiff-($daysLeft*60*60*24)-($hoursLeft*60*60))/60);
$secondsLeft = floor(($dateDiff-($daysLeft*60*60*24)-($hoursLeft*60*60)-($minutesLeft*60)));

Link to comment
https://forums.phpfreaks.com/topic/184328-date-difference-am-i-doing-this-right/
Share on other sites

If it works, it is probably correct. As for doing it different, it looks fine to me as long as it works. The date difference expressed out how you want it is rarely an easy task to accomplish. If you want to compare you code to other functions you can find plenty via google.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.