0

I see there are other posts on this question in other languages, but I can't find one for PHP. I'm not looking for the difference between two date times, but rather how many days different. For example:

2011-08-06 14:05:28

and

2011-08-07 08:46:28

are less then one day apart, but I want a function that will tell me they are on days that are one day apart, i.e. will return a value of 1 for these two dates, and return a value of 2 for the following dates, for example:

2011-08-06 23:05:28

and

2011-08-08 01:46:28

Really, I'm guessing it must be easy, but I've not worked much with dates before.

Oh, and one more constraint, I need something compatible with PHP 5.2, so can't use DateTime::diff.

EDIT: changed to clarify that I do really need a numeric return value, sorry :)

yuttadhammo
  • 5,069
  • 6
  • 34
  • 45
  • 2
    Possibly duplicate http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – CuriousMind Aug 07 '11 at 11:40
  • not sure... I don't need months or years, just number of days, hence the selected answer (instead of 2 years, 3 months, 2 days, I want something like 795) – yuttadhammo Aug 07 '11 at 13:45

6 Answers6

1

if i interpret the question correctly, you need GregorianToJD or mysql to_days

Both functions return a julian day for a specific date, which is basically the number of days since "day 0".

user187291
  • 53,363
  • 19
  • 95
  • 127
1
$date1 = "2011-08-06 14:05:28";
$date2 = "2011-08-07 08:46:28";
if(date('Ymd', strtotime($date1)) == date('Ymd', strtotime($date2)))
{
    // Same day!
}

Update

If you really want the number of days between to dates, I suggest you update to PHP 5.3. Calculating the number of days can be rather difficult, especially if you want to be able to handle days in different years.

$days = abs(date('z', strtotime($date1)) - date('z', strtotime($date2)));

This is the best I can do, but it requires that $date1 and $date2 are in the same year.

Arjan
  • 9,784
  • 1
  • 31
  • 41
0

I would try something like this (using mainly timestamps):

/**
 * @param string $date1
 * @param string $date2
 * @return int
 *   Number of days between the two dates.
 */
function date_diff_days($date1, $date2) {

    // Ignore the time part and convert the date part to timestamp.
    $date1 = substr($date1, 0, 10);
    $date1 = strtotime($date1 . ' 00:00:00');
    $date2 = substr($date2, 0, 10);
    $date2 = strtotime($date2 . ' 00:00:00');

    // Compute the difference in seconds.
    $diff_secs = abs($date1 - $date2);

    // Convert the difference to days.
    $diff_days = ceil($diff_secs / (60 * 60 * 24));

    return $diff_days;

}

I tried it out with both your test cases and it worked just as you wanted. If you want any changes, you can edit the function. Hope it helps!

Jigarius
  • 394
  • 3
  • 16
0
  $date1 = "2011-08-06 14:05:28";
  $date2 = "2011-08-07 08:46:28";
  if(date("Ymd",strtotime($date1)) == date("Ymd",strtotime($date2)) {
      // they match

  } else {
      // calculate the difference
      $diff = ceil(abs(strtotime($date1)-strtotime($date2))/86400); 

  }
blejzz
  • 3,349
  • 4
  • 39
  • 60
0
  1. Convert the dates into timestamps. How this is done depends on the format of the input data.
  2. Format the timestamps using PHP's date function (Ymd seems like a suitable $format).
  3. Compare the resulting strings for equality.

If you need the actual difference in days, call date separately using Y, m and d as formats, construct timestamps from the result (using mktime) and divide the difference between the timestamps by 86400 (the number of seconds per day).

Oswald
  • 31,254
  • 3
  • 43
  • 68
0
$secondsApart = strtotime( '2011-08-07 08:46:28' ) -
                   strtotime( '2011-08-06 14:05:28' );

Now you can compute any other value based on that:

$hoursApart = round( $secondsApart / 3600 );
$daysApart = round( $secondsApart / 86400 );
...

You can also use floor() or ceil() instead round() if that better suits your need.

nobody
  • 10,599
  • 4
  • 26
  • 43