Jump to content
Sign in to follow this  
honkmaster

Date Time Difference Issue

Recommended Posts

Hi, I have two date and time stamps stored in my database, I'm using the below script to calculate the difference but whenever I'm getting an extra 1 hour added ?? Any ideas, Cheers Chris

<?php
$start = $row_rsComplete['quote_added'];
$finish = $row_rsComplete['quote_complete'];
$difference = ($finish - $start);
$day = round(($difference % 604800) / 86400); 
$hours = round((($difference % 604800) % 86400) / 3600); 
$minutes = round(((($difference % 604800) % 86400) % 3600) / 60); 
echo $day." days"."<br/>";
echo $hours." hours"."<br/>";
echo $minutes." minutes"."<br/>";
?>

Share this post


Link to post
Share on other sites

a) it would have been nice if you posted what the input data values were that produce the incorrect result.

 

b) there aren't the same number of hours in each day, especially if you observe Daylight Savings Time in your time zone.

 

c) if you use your database's or php's date/time functions to perform the calculation, it will probably take into account any DST change.

Share this post


Link to post
Share on other sites

Below is code with posted data, result is 1 hr 43 mins which is incorrect, start time is 13:12 and finish is 13:55 which is 43 mins

<?php
$start = '1491480732';
$finish = '1491483308';
$difference = ($finish - $start);
$day = round(($difference % 604800) / 86400); 
$hours = round((($difference % 604800) % 86400) / 3600); 
$minutes = round(((($difference % 604800) % 86400) % 3600) / 60); 
echo $day." days"."<br/>";
echo $hours." hours"."<br/>";
echo $minutes." minutes"."<br/>";
?>
Edited by honkmaster

Share this post


Link to post
Share on other sites

Rounding 0.7 hours yields 1 hour. This is how rounding works. If you want to cut off the decimal fraction, you need floor() and/or an integer type cast.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

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