Jump to content


Photo

Changing Time Stamp into hours minutes seconds?


  • Please log in to reply
6 replies to this topic

#1 craigbabe

craigbabe
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 07 June 2006 - 08:43 AM

Hey I was wondering how I could change the outputed 15600 seconds into the number ofd hours minutes and seconds? I need this for a count down feature.

Is there a function that can do this? I have looked around but only confused myself

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 07 June 2006 - 12:41 PM

No function I know of... you'll just have to do a little math.
$hours = floor($total_secs / 3600);
$min = floor(($total_secs % 3600) / 60);
$sec = floor(($total_secs % 3600) % 60);

Heh... I just came up with that... I might have to use that somewhere.

Info: PHP Manual


#3 obsidian

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

Posted 07 June 2006 - 12:45 PM

[!--quoteo(post=380974:date=Jun 7 2006, 08:41 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Jun 7 2006, 08:41 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
No function I know of... you'll just have to do a little math.
$hours = floor($total_secs / 3600);
$min = floor(($total_secs % 3600) / 60);
$sec = floor(($total_secs % 3600) % 60);

Heh... I just came up with that... I might have to use that somewhere.
[/quote]

lol... very similar to what i wrote up earlier this morning, too, ober... i was a little more verbose to aid in understanding, though:
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));

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

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 07 June 2006 - 12:47 PM

Bah... they didn't ask about days... and I'm results oriented... EFFICIENCY IS KEY! ;-)

Info: PHP Manual


#5 obsidian

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

Posted 07 June 2006 - 02:32 PM

[!--quoteo(post=380977:date=Jun 7 2006, 08:47 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Jun 7 2006, 08:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Bah... they didn't ask about days... and I'm results oriented... EFFICIENCY IS KEY! ;-)
[/quote]

lol... i know, but in the thread i answered with that function, they DID ask about days ;-)
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

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 07 June 2006 - 03:03 PM

I read the post and thinking issint it better to insert the data in this format $count_down_time=date("hms");
and then use a for loop to count down.

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 June 2006 - 03:23 PM

Here's a function I just came up with that uses the standard date() and strtotime() functions:
<?php
function get_hms($secs) {
     if ($secs > 86400) return (false);
     $mid = strtotime(date('Y-m-d 00:00:00',strtotime('now')));
     list($hns['hour'],$hms['min'],$hms['sec']) = explode(':', date('G:i:s',$mid + $secs));
     return($hms);
}?>

Returns an array containing the number of Hours, Minutes, Second or FALSE if the input number is more than a day (86,400 seconds).

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users