HughbertD Posted February 14, 2008 Share Posted February 14, 2008 Hi I have two timestamps from a MySQL DB, a $start and a $end. I want to find the amount of time between them, by subtracting start from end and have done this like so; $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); echo ($conv); For some reason all the results have a day more time than they should on them IS this something to do with strtotime? Thanks for ANY help people can give on this Quote Link to comment Share on other sites More sharing options...
neverett Posted February 14, 2008 Share Posted February 14, 2008 What is the servers timezone? If you call time() does it equal your current time in your timezone? Just a thought. Quote Link to comment Share on other sites More sharing options...
HughbertD Posted February 14, 2008 Author Share Posted February 14, 2008 The server is on the same machine, so there shouldn't be a problem with timezones, thanks for the thought. Quote Link to comment Share on other sites More sharing options...
neverett Posted February 14, 2008 Share Posted February 14, 2008 My guess is that the strtotime() function is taking the current time and returning that because you don't enter a second argument. Take a look at this site (if you haven't already)... http://us3.php.net/manual/en/function.strtotime.php I don't really know a work around though, unless you can get the in unix time (i.e. - $row['dateTime'] is in unix time instead of a string). If I think of something else I'll let you know, in the meantime... good luck! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 14, 2008 Share Posted February 14, 2008 What is the output of this script, just to give us a better idea of what is going on: <?php $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); echo "Start: {$row['dateTime']} ({$start})\n"; echo "End: {$row['dateTimeFin']} ({$end})\n"; echo "Diff: {$result}\n"; echo "Time laps: {$conv}\n"; ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 14, 2008 Share Posted February 14, 2008 After reading your post yesterday, i wrote a function to calculate time difference between two timestamps (either unix timestamps or time strings converted to these). It doesn't use the date() function, so there should be no problems. Have a look: <?php // Modified version of function at http://efficienttips.com/calculate-time-difference-php/ // This function returns an array('years' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds) // If parameters contain only digits, they are treated as unix timestamps, else they are converted using strtotime() // If no $endTime is provided, the function uses the current time // Fails on timestamps prior to ~ 1970 due to unix timestamp limitations function timeDifference($startTime, $endTime = false) { $startTime = ctype_digit($startTime) ? $startTime : strtotime($startTime); $endTime = $endTime ? (ctype_digit($endTime) ? $endTime : strtotime($endTime)) : time(); if ($endTime > $startTime) { $diff = $endTime - $startTime; $years = floor($diff / 31556952); $diff = $diff % 31556952; $months = floor($diff / 2631601.44); $diff = $diff % 2631601.44; $days = floor($diff / 86400); $diff = $diff % 86400; $hours = floor($diff / 3600); $diff = $diff % 3600; $minutes = floor($diff / 60); $diff = $diff % 60; $seconds = $diff; return array('years' => $years, 'months' => $months, 'days' => $days, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds); } else { return 'Error: Start time must be before end time!'; } } // Sample usage $array = timeDifference('2000-01-01 00:00:00'); foreach($array as $unit => $value) { echo $value, ' ', (($value == 1) ? substr($unit, 0, -1) : $unit), '<br />'; } ?> But, since I'm using average seconds per year, month and day, I'm not sure how accurate it is? Cause it says 8 years 1 month 14 days 5 hours 32 minutes 27 seconds from 2000-01-01 00:00:00 to now. That's ten hours off, where I'm at.. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 14, 2008 Share Posted February 14, 2008 Rewrote the function; now it does use gmdate(), and it works great. At least on my box. <?php function timeDifference($startTime, $endTime = false) { $startTime = ctype_digit($startTime) ? $startTime : strtotime($startTime); $endTime = $endTime ? (ctype_digit($endTime) ? $endTime : strtotime($endTime)) : time(); if ($endTime > $startTime) { $diff = $endTime - $startTime; return array('years' => gmdate('Y', $diff) - 1970, 'months' => gmdate('m', $diff) - 1, 'days' => gmdate('d', $diff) - 1, 'hours' => gmdate('H', $diff), 'minutes' => gmdate('i', $diff), 'seconds' => gmdate('s', $diff)); } } // Sample usage $array = timeDifference('2008-02-01 00:00:00'); foreach($array as $unit => $value) { echo $value, ' ', (($value == 1) ? substr($unit, 0, -1) : $unit), '<br />'; } ?> I subtract 1970 from the years and 1 from the months and days, to compensate for the Unix Epoch (1970-01-01 00:00:00 GMT). Seems like that's the source of your problem too! Quote Link to comment Share on other sites More sharing options...
HughbertD Posted February 14, 2008 Author Share Posted February 14, 2008 Thanks Guys! Appreciate the input from everyone, I've been out all day so haven't had a chance to look at the function and toher suggestions, I will do tomorrow. Cheers again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.