Jump to content

Timestamp calculation adding on a day?


HughbertD

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/91045-timestamp-calculation-adding-on-a-day/
Share on other sites

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!

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";
?>

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..

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!

Archived

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

×
×
  • Create New...

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.