Jump to content


Photo

Difference between time() and.....


  • Please log in to reply
2 replies to this topic

#1 philipeddies

philipeddies
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 June 2006 - 08:12 AM

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\" /]

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 June 2006 - 12:12 PM

[!--quoteo(post=380917:date=Jun 7 2006, 04:12 AM:name=Bf2mad)--][div class=\'quotetop\']QUOTE(Bf2mad @ Jun 7 2006, 04:12 AM) View Post[/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:
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));

hope this helps.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 philipeddies

philipeddies
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 June 2006 - 06:24 PM

[!--quoteo(post=380963:date=Jun 7 2006, 01:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 7 2006, 01:12 PM) View Post[/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:
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));

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\" /]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users