Skip to content

How to count days between two dates in PHP?

Last updated on September 17, 2014

In this tutorial, we will write about calculating the number of days between two dates. This can be done very easily using the DateTime object that is available since PHP 5.3.

if you need to count number of days between two time zone read my previous post TimeZone conversion one timeZone to another php

Here is the simple function which will return number of days b/w given dates.

function getNumberOFDays($from,$to){
    $from_date = new DateTime($from);
    $to_date = new DateTime($to);
    return $from_date->diff($to_date)->days;
  //  return $from_date->diff($to_date)->h;
  //  return $from_date->diff($to_date)->i;
  //  return $from_date->diff($to_date)->s;
   // return $from_date->diff($to_date)->invert;
   // return $from_date->diff($to_date)->d;
  //  return $from_date->diff($to_date)->m;
    //return $from_date->diff($to_date)->y;
//DateInterval Object ( [y] => 0 [m] => 0 [d] => 26 [h] => 0 [i] => 0 [s] => 0 [invert] => 0 [days] => 26 ) 
}
echo getNumberOFDays('2014-07-16','2014-08-11');
output : //26

using Above function you can get years,months .. etc. see the commented code.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments