views:

169

answers:

2

I am a complete newbie in PHP. I would like to show the number of days missing for a specific date. In other words, I want to display something like:

"X days to go for the great event"

using PHP and the server time.

How is this accomplished?

+7  A: 
<?php

$event_date = '2010-01-01 00:00:00';
$event_time = strtotime($event_date);
$diff = $event_time - time();
echo floor($diff/(24*60*60)).' days to go for the great event';

Note: I'm totally side stepping any timezone considerations, so, be sure you read up on timezone issues associated with using the PHP datetime functions.

jakemcgraw
Shouldn't it be ceil? If there are 12 hours until midnight of the day of the "great event", most people would consider that a day away.
Matthew Flaschen
Thanks! I am a total newbie. Any good resource for the API?
Mario Ortegón
X = full days until event, if he wanted to make it a little more complicated, he could set a conditional to detect when less than a day remains and echo out a different string.
jakemcgraw
+2  A: 

jakemcgraw's answer would have my vote, had I 15 rep. :)

You might want to use mktime() instead of strtotime(), though.

jensgram
There - got it!
jensgram
As for the API: See http://www.php.net/manual/en/
jensgram