Days since date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hauni
Forum Newbie
Posts: 11
Joined: Thu Sep 25, 2008 11:40 am

Days since date

Post by hauni »

Alright, I have 2 dates.
They first is written like this:
02/03/04
and the second is todays' date:
01/07/10.

Now I wonder, can anyone help me with a script that calculates how many days it has gone (days, not months or weeks) since the first date to the second?
I would be so grateful.
Thanks.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Days since date

Post by mikosiko »

hauni
Forum Newbie
Posts: 11
Joined: Thu Sep 25, 2008 11:40 am

Re: Days since date

Post by hauni »

i want to do it in php, not sql.
Thanks for quick answer.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Days since date

Post by AbraCadaver »

Those dates won't be handled correctly by strtotime(), so something like this:

Code: Select all

$start = explode('/', '02/03/04');
$end = explode('/', '01/07/10');
$days = (mktime(0,0,0,$end[1],$end[0],$end[2]) - mktime(0,0,0,$start[1],$start[0],$start[2])) / 86400; //60sec * 60min * 24hrs 
You can use abs() on the result if the start date can be greater than the end date. Also, you can use round(), ceil() or floor() to round the days as appropriate.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply