honkmaster Posted April 8, 2017 Share Posted April 8, 2017 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/>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/303658-date-time-difference-issue/ Share on other sites More sharing options...
mac_gyver Posted April 8, 2017 Share Posted April 8, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303658-date-time-difference-issue/#findComment-1545190 Share on other sites More sharing options...
honkmaster Posted April 8, 2017 Author Share Posted April 8, 2017 Hi, thanks for response, dateTime is stored as unix Quote Link to comment https://forums.phpfreaks.com/topic/303658-date-time-difference-issue/#findComment-1545194 Share on other sites More sharing options...
honkmaster Posted April 8, 2017 Author Share Posted April 8, 2017 (edited) 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 April 8, 2017 by honkmaster Quote Link to comment https://forums.phpfreaks.com/topic/303658-date-time-difference-issue/#findComment-1545195 Share on other sites More sharing options...
Jacques1 Posted April 8, 2017 Share Posted April 8, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303658-date-time-difference-issue/#findComment-1545196 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.