Jump to content


Photo

Timestamp & date() quandry


  • Please log in to reply
4 replies to this topic

#1 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 23 April 2006 - 05:24 PM

<?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
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 April 2006 - 07:01 PM

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

--> 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
$s = $timestamp%60;
$i = floor($timestamp / 60);
$h = floor ($i / 60);

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 23 April 2006 - 07:07 PM

[!--quoteo(post=367750:date=Apr 23 2006, 03:01 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 23 2006, 03:01 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$timestamp = 1829;
echo date('h:i:s', $timestamp);

--> 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
$s = $timestamp%60;
$i = floor($timestamp / 60);
$h = floor ($i / 60);

echo "$h:$i:$s"; // --> 0:30:29
[/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.
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#4 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 23 April 2006 - 08:14 PM

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;
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 April 2006 - 11:55 PM

or

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


printf ('%d:%02d:%02d', $h,$i,$s);

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
$timestamp = 1829;
echo date ('H:i:s', $timestamp);  // note 'H' and not 'h'

--> 00:30:29

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users