tags:

views:

35

answers:

3

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

+1  A: 
$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
Not sure what you mean with the last phrase. `date_parse` parses dates not intervals in seconds.
Artefacto
@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
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
@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
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
A: 

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
+1  A: 

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
Fatal error: Call to undefined method DateTime::diff()
jason
What version of php are you running?
Russell Dias
I am using php 5.2.4
jason
DateTime is supported since 5.2, some methods require 5.3. If I'm not mistaken.
Russell Dias