Portal Home > Knowledgebase > Articles Database > [php] find elapsed time between 2 future dates


[php] find elapsed time between 2 future dates




Posted by MGCJerry, 01-08-2012, 01:35 AM
I've been looking for code to figure out how many days are between 2 dates in the future. However, I cant use php's built in date functions because the years fall outside of php's date functions. I'm trying to figure a persons age on a given date. Such as, someone born on "November 30, 2098".. How old would this person be on "August 10, 2140" (or any other date in the far future). Does anyone know of a way of figuring out the amount of time between 2 dates?

Posted by quantumphysics, 01-08-2012, 01:48 AM
get it into a 'number of seconds since x (arbitrary event)' format, date2-date1

Posted by babygekko, 01-08-2012, 01:55 AM
Just an example: However - as per php.net The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though. For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

Posted by MGCJerry, 01-08-2012, 12:49 PM
Thanks for the replies guys. Quantumphysics, I've suspected I would have to do that very thing. I may just end up counting the months between the 2 dates, then computing from there to get the years. Right now I'm cheating and only working with the year which really is fine but I'd like to see if I can get more precise by saying for example, 42 years, 6 months and making the code aware of the birth date itself so it doesn't count the year. babygekko, your code works fine as long as the times fall within php's date capabilities. I probably should have specified I'm using Windows 2003 & PHP 5.2.14 on a 32 bit platform. I've seen that concept posted everywhere that uses strtotime(), but for my certain application doesn't work 99% of the time. My overall date range is from 1,00,000 BC to 26,000. Here is the code im presently using... Finds the year using explode() and removes all text (the code supplies the dates which are written as text) and builds an array using the numbers. Then I'm just subtracting the years to get their ages. ex: b. August 16, 2085, to 2140 gives: "(55 Years old)"... I know I could probably use date_parse() instead of explode(), but date_parse() chokes on crazy years such as 6680410 BC. Anyway here's my current code.. Thanks again... I just may end up counting months, but ill have to try later i have to run...



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read

Language: