A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP date calculation - page 1
User InfoPosts
PHP date calculation#1
What is the best (date format independent way) in PHP to calculate difference in days between two dates in specified format.

I tried the following function:

function get_date_offset($start_date, $end_date)
$start_time = strtotime($start_date);
$end_time = strtotime($end_date);
return round(($end_time-$start_time)/(3600*24));

It works ok on linux box, but when running under windows strtotime returns ''.


Input date is in mm/dd/yyyy format, but I would like to make it accept $format as a parameter.

I need only difference in days.

posted date: 2008-12-23 03:33:00

Re: PHP date calculation#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-23 03:33:01

Re: PHP date calculation#3
What's the $start_date and $end_date look like?

posted date: 2008-12-23 03:38:00

Re: PHP date calculation#4
I'm not sure what is considered best, since there is no built-in function in PHP for doing this, but some people have used gregoriantojd(), for example in this forum post.

posted date: 2008-12-23 03:40:00

Re: PHP date calculation#5
Btw - this is not the best way. Consider, for example, subtracting 23:00 22.12.2008 from 01:00 23.12.2008. You'd get 2 hours. Your algorithm will say - 0 days. But it will be the next day!

posted date: 2008-12-23 03:41:00

Re: PHP date calculation#6
The problem with PHP is that it doesn't have a definite DateTime type. You can use a Unix timestamp, or the built-in DateTime class, but they are pretty limited in their functionality. I expect that there should be some 3rd party classes with more extensive support for date-time calculations, but I haven't looked for it.Using Unix timestamps for date (not time) calculations is also tricky. You'd have to discard the time part, but simply resetting to 00:00 is not safe because of daylight savings time (DST). DST has the effect that there are two days every year that don't have exactly 24 hours. Thus, when adding/subtracting dates you might end up with a value that does not divide evenly with 3600*24. I'd suggest looking for some 3rd party class that has proper support for all this stuff. Date/Time calculations are awesome in their ugliness. :P

posted date: 2008-12-23 03:53:00

Re: PHP date calculation#7
The PEAR Date class offers all kinds of features for finding the differences between dates and about 1000 other things as well. The docs for it are here...

posted date: 2008-12-23 03:54:00

Re: PHP date calculation#8
The Zend Framework has the class Zend_Date for dealing with "date math". It works around system specific timestamp limits by using the BCMath extension, or if that's not available limits the timestamps by max float value for your system.// example printing difference in daysrequire('Zend/Date.php');$date1 = new Zend_Date();$date1->set(2, Zend_Date::MONTH);$date1->set(27, Zend_Date::DAY);$date1->set(2008, Zend_Date::YEAR);$date2 = new Zend_Date();$date2->set(3, Zend_Date::MONTH);$date2->set(3, Zend_Date::DAY);$date2->set(2008, Zend_Date::YEAR);echo ($date2->getTimestamp() - $date1->getTimestamp()) / (3600*24);

posted date: 2008-12-23 05:40:00

Re: PHP date calculation#9
If you do not want or you cannot use Zend Framework or Pear package try this function i hope this would help:function dateDifference($date1, $date2){ $d1 = (is_string($date1) ? strtotime($date1) : $date1); $d2 = (is_string($date2) ? strtotime($date2) : $date2); $diff_secs = abs($d1 - $d2); $base_year = min(date("Y", $d1), date("Y", $d2)); $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year); return array ( "years"=> abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)), "months_total"=> (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1, "months"=> date("n", $diff) - 1, "days_total"=> floor($diff_secs / (3600 * 24)), "days"=> date("j", $diff) - 1, "hours_total"=> floor($diff_secs / 3600), "hours"=> date("G", $diff), "minutes_total"=> floor($diff_secs / 60), "minutes"=> (int) date("i", $diff), "seconds_total"=> $diff_secs, "seconds"=> (int) date("s", $diff) );}

posted date: 2008-12-23 06:56:00

Re: PHP date calculation#10
Let's not overengineer, guys.$d1 = strtotime($first_date);$d2 = strtotime($second_date);$delta = $d2 - $d1;$num_days = ($delta / 86400);

posted date: 2008-12-23 10:57:00

Re: PHP date calculation#11
Yep, definitely the best answer so far.

posted date: 2008-12-23 11:43:00

Re: PHP date calculation#12
It's the same as in question. Problem is that strtotime doesn't work on windows box the same way as on linux, and that it can't parse arbitrary date formats.

posted date: 2008-12-24 04:01:00

select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.