Sebastjan
Sebastjan

Reputation: 1147

php DateTime countdown

So I have a question about a DateTime in php.

$datetime1 = new DateTime('2013-02-01 10:40:00');
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');

What echo outputs is: 2day 8hours 33minutes. Ok I know that the difference between first and the second variable is equal to the output. But is there any way that the output could be some sort of count down.

For example:

$datetime1 = new DateTime('2013-01-01 00:00:00');
$datetime2 = new DateTime('2013-01-01 13:30:00');

What I want to be output is: 13:30:00, and 2 minutes later there would be 13:28:00.

Is there any way to be done that with diff function.

Thanks for help

Sebastian

Upvotes: 1

Views: 4654

Answers (1)

John Conde
John Conde

Reputation: 219924

This will only work if:

  • one of the times in now
  • the page refreshes or you use ajax. PHP is executed on the server-side.

So you basically already have the code with just a tweak:

$datetime1 = new DateTime();
$datetime2 = new DateTime('2013-02-03 19:13:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d day %h hours %i minutes');

Upvotes: 3

Related Questions