Home  >  Article  >  Daily Programming  >  How to implement countdown function using PHP? (Pictures + Videos)

How to implement countdown function using PHP? (Pictures + Videos)

藏色散人
藏色散人Original
2018-09-28 14:39:0310484browse

This article mainly introduces to you how to implement a simple countdown function in PHP.

In our daily lives, countdowns are almost everywhere in a sense, such as Taobao limited-time shopping, college entrance examination countdowns, holiday countdowns, and so on. During the development of our website, we may also encounter the need to implement the countdown function.

It may be a little difficult for new PHP users, but as long as you master the implementation principle of countdown, it will be easy to implement.

Let’s introduce the method of

PHP to implement the countdown function through a simple code example.

PHP implementation of countdownThe code example is as follows:

<?php
$time1 = time();
$time2 = strtotime("2018-10-1");
$time3 = strtotime("2018-10-1");
$sub = ceil(($time2 - $time1)/3600);
$sub2 = ceil(($time3 - $time1)/86400);

echo "距离十一放假还有$sub" . "小时!" . &#39;<br>&#39;;
echo "距离十一放假还有$sub2" . "天!" . &#39;<br>&#39;;

In this method we access through the browser, the countdown effect is as follows:

How to implement countdown function using PHP? (Pictures + Videos)

As shown in the picture, here we implement the countdown function to eleven.

In the above code, we first use the

time function to obtain the current timestamp, and then use the strtotime function# for the expected normal time of arrival, which is "2018-10-1" ##Resolved as Unix timestamp. time: Returns the current Unix timestamp.

strtotime: Parses any string datetime description into a Unix timestamp.

Then use the formula:

($time2 - $time1)/3600

Calculate how many hours there are between the current time and November. 3600 represents the number of seconds in an hour.

($time3 - $time1)/86400

Calculate how many days are left between the current time and National Day. 86400 represents the number of seconds in a day.

Note

: We need to use the ceil function to round the calculation results.

ceil means rounding to the nearest integer.

This article is about a simple method to implement the countdown function in PHP. I hope it will be helpful to friends in need!

If you want to learn more about PHP, you can follow the PHP Chinese website

PHP Video Tutorial

, everyone is welcome to refer to and learn!

The above is the detailed content of How to implement countdown function using PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn