![]() |
Finding the distance between 2 dates...
Hi all.
Im trying to find a method (if there is any) that can calculate the distance in days, months or years between two dates. Mainly the distance in days. i almost searched the whole datetime object to no afail... So for example i have 2008-05-29 and 2008-06-01 i would like to find the distance in days for instance which should be 3. or in months wich should be 1 or years that should be 0. Usually i use mysql to solve this little math problem, but i was wondering if there isnt a php method that could calculate this instead of using de expensive sql connection all the time to solve it... Hope its kinda clear what i mean... Thanks in advance, May |
|
Although Adam's way is more thorough, you might want to use this.
PHP Code:
|
@up- So why do you use sEndDate ? You don't even use it anywhere xD
|
There you go.
|
Hehe thanks guys for solving this using pure math. Im sure i can put it to use at some point so thx for that :D
But still there isnt a method out there that actually solves this within an php function like Mysql can using DATEDIFF('expr1', 'expr2')? |
I'm unaware of any such function. Maybe there is one in-built though, but I somehow doubt it as it's quite basic mathematics.
|
You can use the DATE() function in mySQL though.
|
Wildhoney, I apologize in advance if stealing.. er, borrowing your code is in any way offensive, but just for clarity's sake;
PHP Code:
-m |
That's quite OK! How about the following?
php Code:
Then we can use it like so: php Code:
|
Show off.
Excellent example of using __call() though. -m |
How about calculating the correct answers when a daylight savings boundary is crossed? For example, at the moment, the code will return 0 days between March 30th and 31st this year (for timezone Europe/London).
|
Hehehe is it me? or are subjects always treated this thurally?
Btw great examples there :) ow and... :P When do we cross the point that we are actually discussing how to handle leap years and the so much discussed 4000 Years rule in the gregorian calendar? (not hoping my code wil ever run that long :P) Hehe. Anywayz thanks in advanced for the comments on this subject :D regards, |
i think you can use the date function to output the timezone offset in seconds by using the 'Z' param:
PHP Code:
PHP Code:
|
I think this should do it, there is probably a better way of doing it but it works for me:
PHP Code:
edit: i think this is for php 5.2.x not sure about 5.1.x as i dunno when these classes were added, any1 know? |
sketch, try that with some other timezone settings applied to the DateTime objects. For example, at the top of the script call
date_default_timezone_set('Asia/Tokyo');. |
with the date_default_timezone_set('Asia/Tokyo')
set it outputs: 1.04166666667 but if you also change PHP Code:
PHP Code:
this should do it automatically: PHP Code:
|
Combined into Wildhoney's class (which i have slightly modified, i added 'array_key_exists, instead of 'in_array' and a few small things)
PHP Code:
PHP Code:
|
I just want to point out something in the above classes
PHP Code:
|
| All times are GMT. The time now is 04:06 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0