Jump to content

Difference between time() and.....


philipeddies

Recommended Posts

Hi,

I am having trouble displaying the difference between the current time and another time ie 1149580156

I want to display in this format

5 days 15hours 10mins 15seconds

and I would like to like to use strftime if possible.

any ideas?

Thanks
Phil [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Link to comment
Share on other sites

[!--quoteo(post=380917:date=Jun 7 2006, 04:12 AM:name=Bf2mad)--][div class=\'quotetop\']QUOTE(Bf2mad @ Jun 7 2006, 04:12 AM) [snapback]380917[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi,

I am having trouble displaying the difference between the current time and another time ie 1149580156

I want to display in this format

5 days 15hours 10mins 15seconds

and I would like to like to use strftime if possible.

any ideas?

Thanks
Phil [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
[/quote]

strftime() only will let you format an actual timestamp, not the difference between two. you will have to do such calculations manually. a function like this may help you out:
[code]
function getTimeDiff($ts1, $ts2) {
  $diff = abs($ts1 - $ts2);

  $sec = 1;
  $min = $sec * 60;
  $hour = $min * 60;
  $day = $hour * 24;

  $dayDiff = floor($diff / $day);
  $diff = $diff % $day;
  $hourDiff = floor($diff / $hour);
  $diff = $diff % $hour;
  $minDiff = floor($diff / $min);
  $secDiff = $diff % $min;
  
  return "$dayDiff days $hourDiff hours $minDiff mins $secDiff seconds";
}

echo getTimeDiff(time(), mktime(0,0,0,12,2,2005));
[/code]

hope this helps.
Link to comment
Share on other sites

[!--quoteo(post=380963:date=Jun 7 2006, 01:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 7 2006, 01:12 PM) [snapback]380963[/snapback][/div][div class=\'quotemain\'][!--quotec--]
strftime() only will let you format an actual timestamp, not the difference between two. you will have to do such calculations manually. a function like this may help you out:
[code]
function getTimeDiff($ts1, $ts2) {
  $diff = abs($ts1 - $ts2);

  $sec = 1;
  $min = $sec * 60;
  $hour = $min * 60;
  $day = $hour * 24;

  $dayDiff = floor($diff / $day);
  $diff = $diff % $day;
  $hourDiff = floor($diff / $hour);
  $diff = $diff % $hour;
  $minDiff = floor($diff / $min);
  $secDiff = $diff % $min;
  
  return "$dayDiff days $hourDiff hours $minDiff mins $secDiff seconds";
}

echo getTimeDiff(time(), mktime(0,0,0,12,2,2005));
[/code]

hope this helps.
[/quote]

Perfect, works great!!!

Thanks again
[img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
Link to comment
Share on other sites

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.