Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When a user signs up on my site they have to select a date as part of resignation.

On their profile page I need to show a countdown to that date. Something simple like attached. How can I create this function?

enter image description here

share|improve this question
1  
I would start by searching the web for php subtracting dates. –  Milo Apr 2 '13 at 2:49
    
While you might be operating in a WP-environment, I fail to see what aspect of this is specific to that. Hence: How is this on-topic on this stack? –  Johannes Pille Apr 2 '13 at 7:38

2 Answers 2

You can use the following function to display it

<?php
function days_to_go(){
    $datetime1 = new DateTime('2013-3-2');//Pass resignation date as a parameter.
    $datetime2 = new DateTime(now);
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%a days to go');
}
?>

For more information on date_diff function see this page.

share|improve this answer

I just found this question when looking for a function to create a dynamic countdown and I guess AJAX is the solution for me. I modified some code I found on a website (do not remember which one) and I'll post it here with the hope it will be useful to somebody. Here's the code:

date_default_timezone_set('America/Mexico_City'); //For some reason I needed this to get the number of hours right, it seems like strtotime and time() get different time zones if you don't specify a time zone
$nextThursday = strtotime("next Friday, 01:00 PM");
$remaining = $nextThursday - time(); //Calculating the number of seconds between no and the specified future date
$days_remaining = floor($remaining / 86400); //Dividing between the number of seconds in a day to get number of days
$hours_remaining = floor(($remaining % 86400) / 3600); //Getting the number of seconds that didn't complete a day and dividing them between the number of seconds in an hour to get the hours remaining
$minutes_remaining = floor((($remaining % 86400) % 3600) / 60);
$seconds_remaining = (($remaining % 86400) % 3600) % 60;
echo "There are $days_remaining days $hours_remaining hours $minutes_remaining minutes left and $seconds_remaining seconds left";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.