i8grand Posted May 25, 2010 Share Posted May 25, 2010 I have two timestamps, $startTime and $endTime . How do I go about creating a $totalTime from these two? Is it just a case of $totalTime = ($endTime - $startTime); ? Thanks, Dylan. Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/ Share on other sites More sharing options...
Mchl Posted May 25, 2010 Share Posted May 25, 2010 It is Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063116 Share on other sites More sharing options...
i8grand Posted May 25, 2010 Author Share Posted May 25, 2010 its not working just doing it like that :S $dbQuery = "SELECT startTime, endTime FROM puzzlestatus WHERE puzzleID=$puzzle"; $result = mysql_query($dbQuery,$db); $dbRow = mysql_fetch_array($result); $start = $dbRow[0]; $end = $dbRow[1]; echo "Start: $start"."<br>"; echo "End: $end"."<br>"; $totalTime = ($end-$start); echo "Total: $totalTime"."<br>"; $start and $end both come echo what the correct timestamps but $totalTime just echoes 0. Am i doing something silly, can anyone help? Dylan. Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063137 Share on other sites More sharing options...
kenrbnsn Posted May 25, 2010 Share Posted May 25, 2010 What types are those fields? "DATE", "DATETIME", "TIMESTAMP", something else? Ken Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063140 Share on other sites More sharing options...
i8grand Posted May 25, 2010 Author Share Posted May 25, 2010 they are TIMESTAMP Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063142 Share on other sites More sharing options...
Mchl Posted May 25, 2010 Share Posted May 25, 2010 A valid question. The word 'timestamp' without any more context is often assumed to mean Unix timestamp - a number of secoonds since 1970-01-01 GMT. If it's MySQL's TIMESTAMP, it get's returned as Y-M-D H:M:S format, which obviously cannot be treated arithmetically. You can do: $totalTime = strtotime($endTime) - strtotime($startTime); to get number of seconds, then extract seconds/minutes/hours/days Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063147 Share on other sites More sharing options...
i8grand Posted May 25, 2010 Author Share Posted May 25, 2010 thanks, how do I then turn that back into formatted timestamp? Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063174 Share on other sites More sharing options...
Mchl Posted May 25, 2010 Share Posted May 25, 2010 $totalTime = strtotime($endTime) - strtotime($startTime); //let's say it's 123456 $days = floor($totalTime / (24 * 3600)); // there are 3600 seconds in an hour $totalTime = $totalTime % (24 * 3600); $hours = floor($totalTime / 3600); $totalTime = $totalTime % (3600); $minutes = floor($totalTime / 60); $seconds = $totalTime % 60; $time = "$days : $hours : $minutes : $seconds"; //'1 : 10 : 17 : 36 ' Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063176 Share on other sites More sharing options...
i8grand Posted May 25, 2010 Author Share Posted May 25, 2010 THATS GREAT, GOT IT GOING THANKS, dYLAN. Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063197 Share on other sites More sharing options...
i8grand Posted May 25, 2010 Author Share Posted May 25, 2010 Sorry for capitals, should probably proof read things before I send them! Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063198 Share on other sites More sharing options...
Mchl Posted May 25, 2010 Share Posted May 25, 2010 Happens to everyone. You have the ability to edit your messages within 10 minutes from posting. Link to comment https://forums.phpfreaks.com/topic/202859-calculations-with-timestamp/#findComment-1063214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.