views:

150

answers:

2

Is there anyway to find the date difference in php? I have the input of from date 2003-10-17 and todate 2004-03-24. I need the results how many days is there within these two days. Say if 224 days, i need the output in days only.

I find the solution through mysql but i need in php. Anyone help me, Thanks in advance.

+1  A: 

You can use the parse timestamp feature to convert dates to timestamps, subtract the timestamps, and then convert the resulting timestamp (seconds) to days:

floor((strtotime("2004-03-24") - strtotime("2003-10-17"))/86400);
Luis
Thanks nice answer this
Karthik
+4  A: 
$start = new DateTime( '2003-10-17' );
$end   = new DateTime( '2004-03-24' );
$diff  = $start->diff( $end );

echo $diff->format( '%d days' );

...should do it.

For reference see DateTime and DateInterval.

Mind you though, this is only available as of PHP 5.3.

fireeyedboy
I got this type of error :Fatal error: Class 'DateTime' not found
Karthik
@Karthik: yes, sorry... I should have mentioned it's only available as of PHP 5.3.
fireeyedboy
I would use DateTime::createFromFormat('!Y-m-d', '2003-10-17') mainly because I don't like depending on strtotime magic functionality. I always feel like it will get the month and day mixed up, even though it seems to work properly with yyyy-mm-dd format.
chris
@chris: I hear what you are saying. Good point.
fireeyedboy
Oh ok thanks a lot fireeyedboy. I will try in php 5.3.
Karthik