Countdown to date function?

时间:2013-04-02 作者:js111

当用户在我的网站上注册时,他们必须选择一个日期作为辞职的一部分。

在他们的个人资料页面上,我需要显示该日期的倒计时。一些简单的东西,比如附件。如何创建此函数?

enter image description here

2 个回复
SO网友:Vinod Dalvi

您可以使用以下功能来显示它

<?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\');
}
?>
有关date\\u diff函数的更多信息,请参阅this page.

SO网友:Eduardo Sánchez Hidalgo Urías

我在寻找创建动态倒计时的函数时发现了这个问题,我想AJAX是我的解决方案。我修改了在一个网站上找到的一些代码(不记得是哪一个),我将把它发布在这里,希望它对某些人有用。代码如下:

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";

结束