简体   繁体   English

PHP倒计时到目前为止

[英]PHP Countdown to Date

How could set a date and get a countdown in PHP? 如何设置日期并在PHP中倒计时? For example if I set the date as 3 December 2PM it would tell me how many days and hours are remaining. 例如,如果我将日期设置为12月3日下午2点,它会告诉我剩余的天数和小时数。

No need for user inputs for the date as it will be hard coded. 不需要用户输入日期,因为它将被硬编码。

Thanks. 谢谢。

You can use the strtotime function to get the time of the date specified, then use time to get the difference. 您可以使用strtotime函数来获取指定日期的时间,然后使用时间来获得差异。

$date = strtotime("December 3, 2009 2:00 PM");
$remaining = $date - time();

$remaining will be the number of seconds remaining. $剩余将是剩余的秒数。 Then you can divide that number to get the number of days, hours, minutes, etc. 然后你可以将这个数字除以得到天数,小时数,分钟数等。

$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
echo "There are $days_remaining days and $hours_remaining hours left";

Let me play around like this: 让我这样玩:

$rem = strtotime('2012-08-01 14:00:00') - time();
$day = floor($rem / 86400);
$hr  = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day Days ";
if($hr) echo "$hr Hours ";
if($min) echo "$min Minutes ";
if($sec) echo "$sec Seconds ";
echo "Remaining...";

Try this at your leisure... :-) 在闲暇时试试这个...... :-)

NOTE: There is no if() test for echo "Remaining..." , just coz you wont process this in case when $rem <= 0 . 注意:没有if()测试echo "Remaining..." ,因为如果$rem <= 0 ,你不会处理它。 Isn't it? 不是吗?

PHP 5.3 allows this: PHP 5.3允许这样:

$dt_end = new DateTime('December 3, 2009 2:00 PM');
$remain = $dt_end->diff(new DateTime());
echo $remain->d . ' days and ' . $remain->h . ' hours';

It's not as trivial as subtracting strtotime() results, since there are daylight savings and time would be mathematically correct, but not physically. 它不像减去strtotime()结果那样微不足道,因为有白天节省,时间在数学上是正确的,但不是物理上的。 Anyway, for these purposes you should use gmdate() function, which has no daylight savings: 无论如何,出于这些目的,你应该使用gmdate()函数,它没有夏令时:

$date = gmdate('U', strtotime('2009-12-03 14:00'));

// Get difference between both dates without DST
$diff = $date - gmdate('U');
// Days (in last day it will be zero)
$diff_days = floor($remaining / (24 * 60 * 60));
// Hours (in the last hour will be zero)
$diff_hours = floor($remaining % (24 * 60 * 60) / 3600);

Did this countdown until the end of the semester: 这个倒计时直到学期结束:

    $endOfSemester = mktime(15,30,0,5,21,2015);
    $now = time();
    $secondsRemaining = $endOfSemester - $now;

    define('SECONDS_PER_MINUTE', 60);
    define('SECONDS_PER_HOUR', 3600);
    define('SECONDS_PER_DAY', 86400);

    $daysRemaining = floor($secondsRemaining / SECONDS_PER_DAY); //days until end
    $secondsRemaining -= ($daysRemaining * SECONDS_PER_DAY); //update variable

    $hoursRemaining = floor($secondsRemaining / SECONDS_PER_HOUR); //hours until end
    $secondsRemaining -= ($hoursRemaining * SECONDS_PER_HOUR); //update variable

    $minutesRemaining = floor($secondsRemaining / SECONDS_PER_MINUTE); //minutes until end
    $secondsRemaining -= ($minutesRemaining * SECONDS_PER_MINUTE); //update variable

    echo("<h3>There are $daysRemaining days, $hoursRemaining hours, $minutesRemaining minutes, $secondsRemaining seconds until the end of the semester</h3>"); //print message

Using @Izhar Aazmi solution, you could set this up nicely for display, as such: 使用@Izhar Aazmi解决方案,您可以很好地进行显示,如下所示:

    public function countdown($time, $h = true, $m = true, $s = true) {
        $rem = $time - time();
        $day = floor($rem / 86400);
        $hr = floor(($rem % 86400) / 3600);
        $min = floor(($rem % 3600) / 60);
        $sec = ($rem % 60);

        if ( $day && !$h ) {
            if ( $hr > 12 ) $day++; // round up if not displaying hours
        }

        $ret = Array();
        if ( $day && $h ) $ret[] = ($day ? $day ." day".($day==1?"":"s") : "");
        if ( $day && !$h ) $ret[] = ($day ? $day . " day" . ($day == 1 ? "" : "s") : "");
        if ( $hr && $h ) $ret[] = ($hr ? $hr ." hour" . ($hr==1?"":"s") : "");
        if ( $min && $m && $h ) $ret[] = ($min ? $min ." minute". ($min==1?"":"s") : "");
        if ( $sec && $s && $m && $h ) $ret[] = ($sec ? $sec ." second".($sec==1?"":"s") : "");

        $last = end($ret);
        array_pop($ret);
        $string = join(", ", $ret)." and {$last}";

        return $string;
    }

I hope this helps! 我希望这有帮助! It's a nice clean way or displaying the countdown. 这是一个很好的干净方式或显示倒计时。

For those looking for a function capable of handling larger and smaller time span (php >5.3) : 对于那些寻找能够处理更大和更小时间跨度的功能的人(php> 5.3):

/**
 * Return a textual representation of the time left until specified date
 */
function timeleft(DateTime $date){
  $now = new DateTime();

  if($now > $date){
    return '0 second';
  }

  $interval = $date->diff($now);

  if($interval->y){
    return $interval->format("%y year").($interval->y > 1 ? 's':'');
  } else if($interval->m){
    return $interval->format("%m month").($interval->m > 1 ? 's':'');
  } else if($interval->d){
    return $interval->format("%d day").($interval->d > 1 ? 's':'');
  } else if($interval->h){
    return $interval->format("%h hour").($interval->h > 1 ? 's':'');
  } else if($interval->i){
    return $interval->format("%i minute").($interval->i > 1 ? 's':'');
  } else if($interval->s) {
    return $interval->format("%s second").($interval->s > 1 ? 's':'');
  } else {
    return 'milliseconds';
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM