Solved

php datetime difference

Posted on 2014-11-03
9
217 Views
Last Modified: 2014-11-03
Dear All
I want to calculate the difference days between two dates,  then I want to use function ceiling (ceil) in the end.
but I found strange case , please copy the code and see the result for both samples, the difference only between these two samples I changed the month
both samples it should give me same result.
what is my mistake in this code ?
what is the best way to calculate the difference days between two dates?



<style type="text/css">table, td {border: 1px solid black;border-collapse: collapse;padding: 5px;}</style>
<?php 
$from_time1 = strtotime("2014-10-22 17:00:00");
$to_time1   = strtotime("2014-10-26 16:55:00");
$from_time2 = strtotime("2014-11-22 17:00:00");
$to_time2   = strtotime("2014-11-26 16:55:00");
?> 
 <TABLE>
 	<tr>
 	<td> No#</td>
 	<td> From Date Time</td>
 	<td> To  Date Time</td>
 	<td> Result </td>
 	<td> Ceil() </td>
 	</tr>
 	<tr>
 	<td> 1 </td>
 	<td> <?php echo "2014-10-22 17:00:00"; ?> </td>
 	<td> <?php echo "2014-10-26 16:55:00"; ?> </td>
 	<td> <?php echo  (($to_time1-$from_time1) / 86400 ). " Days"; ?> </td>
 	<td> <?php echo  Ceil(($to_time1-$from_time1) / 86400 ). " Days"; ?> </td>
 	</tr>
 	<tr>
 	<td> 2 </td>
 	<td> <?php echo "2014-11-22 17:00:00"; ?> </td>
 	<td> <?php echo "2014-11-26 16:55:00"; ?> </td>
 	<td> <?php echo  (($to_time2-$from_time2) / 86400 ). " Days"; ?> </td>
 	<td> <?php echo  Ceil(($to_time2-$from_time2) / 86400 ). " Days"; ?> </td>
 	</tr>
 </TABLE>

Open in new window

0
Comment
Question by:zoscoit
9 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40419643
I get the same answers for both (see below)Screenshot - what should I be seeing?
0
 
LVL 58

Expert Comment

by:Gary
ID: 40419644
If all you are wanting is the days difference then use the date_diff function
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40419648
This question and many others about Date/Time processing is addressed in the article here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

To clarify, when you want the differences between two dates, you probably do not want to take the time of day into account.  It might seem intuitive that you can just say a day has 86,400 seconds (60*60*24) but that is not true on at least two days of the year if you perform your calculations in a zone that uses daylight savings time.  And there are leap years, leap seconds, etc., to further complicate things.  So use the built-in functions as much as possible and avoid making the computations yourself.
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 1

Author Comment

by:zoscoit
ID: 40419718
My resultI have this result why ?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40419729
What are your timezone settings?
0
 
LVL 1

Author Comment

by:zoscoit
ID: 40419732
Gary 2014-11-03 at 19:03:56

Please make it simple and show me how?


 
Ray Paseur 2014-11-03 at 19:05:46

what is you mean by the built-in functions
0
 
LVL 1

Author Comment

by:zoscoit
ID: 40419749
What are your timezone settings?
timezoneBut actually I am in Arabia Time Zone (UTC+03:00)
0
 
LVL 1

Author Comment

by:zoscoit
ID: 40419769
it solved by add one line in php file

date_default_timezone_set("Asia/Riyadh");

Open in new window

0
 
LVL 1

Author Closing Comment

by:zoscoit
ID: 40419771
add one line to php file
date_default_timezone_set("Asia/Riyadh");
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question