4

I have two timestamp input one is the current time and another one is future

i.e.

Future time: 2010-8-17 23:00 Present time: 2010-8-15 11:00

I want to setup notification system which will display the time intervals between the above dates. i.e.

15 minutes before 30 minutes before 1 hour before 2 hour before 3 hour before .... .... .... 1 day before

I am not sure how to achieve this task in php, wondering if any one here can suggest me how to achieve this task

Maximus
  • 2,906
  • 4
  • 35
  • 55
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – YakovL Apr 11 '18 at 10:42

4 Answers4

3

How about using the DateTime class.

<?php
$datetime1 = new DateTime('2010-8-15 11:00');
$datetime2 = new DateTime('2010-8-17 23:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days, %H hours');
?>

Output:

+2 days, 12 hours

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
1
$t1 = getdate($current_date);
$t2 = getdate($future_date);
return $t2[0]-$t1[0];

This will give you the difference between the two dates, measured in seconds.

Borealid
  • 95,191
  • 9
  • 106
  • 122
  • Not sure what you mean with the last phrase. `date_parse` parses dates not intervals in seconds. – Artefacto Aug 16 '10 at 00:41
  • @Artefacto: Deleted. `date_parse` *will* accept a unix timestamp if you prepend '@' to it, but you'd then have to subtract the `parse_date(@0)` to make the output be the difference between the two given dates. – Borealid Aug 16 '10 at 00:51
  • I'm still not seeing how this helps. Perhaps you could give an example on how this strategy can result in what the OP wants. – Artefacto Aug 16 '10 at 00:53
  • @Artefacto: `$my_diff_from_epoch = date_parse('@'.($t2[0]-$t1[0])); $epoch = date_parse('@0'); $my_year_diff = $my_diff_from_epoch['year']-$epoch['year']; $my_month_diff = $my_diff_from_epoch['month']-$epoch['month'];` etc. – Borealid Aug 16 '10 at 00:58
  • That won't work. Let's say we're comparing "2010-01-01 00:00" with "2009-12-31 23:59". The difference should be "1 seconds", but you'll get "1 year, -11 months", etc. – Artefacto Aug 16 '10 at 01:08
0

Get the difference between the two dates and then use date_parse

// $difference = difference in times
print_r(date_parse($difference));

That will result in something along the lines of

Array ( [year] => 2006 [month] => 12 [day] => 12 [hour] => 10 [minute] => 0 [second] => 0 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array() [error_count] => 0 [errors] => Array() [is_localtime] => )

See http://php.net/manual/en/function.date-parse.php for more info

Parker
  • 8,539
  • 10
  • 69
  • 98
0

This is what I did when I needed the exact same thing.

You can easily extend it by getting the number of seconds in a week, month, or year.

function getTimeInterval($ts1, $ts2)
{
    $interval = (int)$ts2 - (int)$ts1;
    if ( $interval < 0 ) {
        return false;
    } else {
        $days = floor($interval / 86400); // seconds in one day
        $interval = $interval % 86400;
        $hours = floor($interval / 3600);
        $interval = $interval % 3600;
        $minutes = floor($interval / 60);
        $interval = $interval % 60;
        $seconds = $interval;
    }
    return array(
        'days' => $days,
        'hours' => $hours,
        'minutes' => $minutes,
        'seconds' => $seconds
    );
}
elpadi
  • 76
  • 6