Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Unix timestamps into readable days/hours/seconds ACCURATELY

Status
Not open for further replies.

HughbertD

Technical User
Apr 30, 2007
42
GB
Hi all,

I have the following code,

Code:
$start = strtotime($row["dateTime"]);
$end = strtotime($row["dateTimeFin"]);
				
$result = ($end - $start);
		
$conv=gmdate("d \d\a\y\s H \h\o\u\\r\s\ i \m\i\\n\s\ s \s\e\c\s",$result);

All my results are a day out. When I find out the months and years, there are 70 extra years and 1 extra month as well!

I don't understand why this happens, I can't seem to accurately subtract 70 years, 1 month and a day either

Any help would be really appreciated
 
what are the values of the two $row elements?
 
Whatever is in the database, but for example it could be,

2008-02-25 12:00:00

2008-02-26 13:00:00

Sorry about the late reply, my email notification is off I think
 
well the dates look well formed so i assume that the conversion to a unix timestamp is accurate.

the maths is fine too. this will give you an intervening number of seconds.

however, you cannot make a date of this using gmdate since this requires a timestamp. all you are giving it is a number of seconds (which it will infer as being from 1/1/1970).

instead you need to resort to mathematics and break the seconds down into mins/hours etc. this is not straightforward if you have to take into account leap years, daylight savings time etc.

it is better to use mysql to do the calculation - check out the mysql forum for help with this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top