Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.