-1

Possible Duplicate:
How to calculate the difference between two dates using PHP?

How I can get number of days between 2 dates using PHP? eg:

25\02\2011
25\03\2011

I want to make PHP calculation: how many days are between 25\02\2011 and 25\03\2011.

Community
  • 1
  • 1
Kareem Nour Emam
  • 1,054
  • 4
  • 14
  • 27

1 Answers1

4
<?php
echo date_diff(new DateTime('2011-02-25'), 
    new DateTime('2011-03-25'))->format('%a');

This is an Object Oriented approach, that is much more reliable and simple than some of the alternatives (using the differences in the unix timestamp, which works in most cases).

date_diff is an alias of DateTime::diff, so we're creating two DateTime objects and using date_diff to return a DateInterval object.

The ->format('%a') is just asking for the interval using the total number of days. You can make it more informative with a longer format to print: x years, x months, x days

Jacob
  • 8,278
  • 1
  • 23
  • 29