Jump to content

Archived

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

phporcaffeine

Timestamp & date() quandry

Recommended Posts

<?php

//$TIMESTAMP IS THE RESULT OF THE SUBTRACTION OF TWO TIMESTAMPS

//THE GOAL IS TO GET THE DIFFERENCE BETWEEN TWO TIMESTAMPS AND THEN USE DATE() TO FORMAT IT
//INTO SOMETHING PEOPLE THAT CAN'T COUNT SECONDS CAN READ.

$timestamp = 1829;

echo date(h:i:s, $timestamp);

//The result is that date() grabs the seconds and minutes right but for the hour, it will consistantly return the
//number ' 7 ', even if the stamp hasn't reached a single hour yet

?>

Anyone have any ideas?

P.S in php.ini I have my timezone = US/Eastern

Share this post


Link to post
Share on other sites
[code]$timestamp = 1829;
echo date('h:i:s', $timestamp);[/code]

--> 12:30:29
The 5 hour difference between our results will be the timezone diff (I'm GMT +0 and you are GMT -5)

try
[code]
$s = $timestamp%60;
$i = floor($timestamp / 60);
$h = floor ($i / 60);

echo "$h:$i:$s"; // --> 0:30:29
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=367750:date=Apr 23 2006, 03:01 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 23 2006, 03:01 PM) [snapback]367750[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]$timestamp = 1829;
echo date('h:i:s', $timestamp);[/code]

--> 12:30:29
The 5 hour difference between our results will be the timezone diff (I'm GMT +0 and you are GMT -5)

try
[code]
$s = $timestamp%60;
$i = floor($timestamp / 60);
$h = floor ($i / 60);

echo "$h:$i:$s"; // --> 0:30:29
[/code]
[/quote]


!@# PERFECTO !@#

Now, for my own edumaction, could you explain why I wasn't able to do it with date()?

I thought of one other thing -

lets say the timestamp if formatted and it comes out to be 0:59:59.

in a momeny it will read 1:60:xx then 1:61:xx is there an easy way to make it so that at 60 minutes it rolls back to 0 minutes and bumps up the hour ( the same goes for seconds ).

Thats why I was trying to use date() for that, because it will ' automagically ' do that.

Share this post


Link to post
Share on other sites
Here is the solution to my last question if anyone cares:

$s = $savedstamp%60;

$i = floor($savedstamp / 60);

$h = floor ($i / 60);


//This takes the minutes subtracted by the number of hours X 60
//In effect, the minute column will never show above 60 minutes but will still be in correct sync with the
//seconds and the hour(s)

$real_i = $i - $h * 60;

Share this post


Link to post
Share on other sites
or

[code]
$timestamp = 3599+1;
$h = floor ($timestamp / 3600);
$timestamp %= 3600;
$i = floor($timestamp / 60);
$s = $timestamp%60;


printf ('%d:%02d:%02d', $h,$i,$s);
[/code]

As for your other question, I'm not sure why I got 12 hrs and not 0 (GMT) but I was expecting the 5 hour difference just because I've seen the same problem before.

EDIT

I just tried
[code]$timestamp = 1829;
echo date ('H:i:s', $timestamp);  // note 'H' and not 'h'[/code]

--> 00:30:29

Share this post


Link to post
Share on other sites

×

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.