Detailed explanation of functional examples of countdown using PHP time and date function strtotime()

怪我咯
Release: 2023-03-07 14:42:01
Original
8672 people have browsed it

We used the strtotime() function earlier to implement the function of comparing the size of two times. In addition to comparing the size of two dates, PHP can also accurately calculate the two dates. difference. In this chapter, we still use the strtotime() function to develop a countdown function.

strtotime() function is to parse the date and time of any English text into a UNIX timestamp. We have explained it before, and we will not introduce it too much here. Just look at the code.

Countdown applet example, the code is as follows

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码

$time1=strtotime(date("Y-m-d H:i:s"));                              //获取当前时间
$time2=strtotime("2017-5-1");                              //五一放假时间
$time3=strtotime("2017-5-5");                              //端午放假时间

$sub1=ceil(($time2-$time1)/3600);                            //(60秒*60分)秒/小时
$sub2=ceil(($time3-$time1)/86400);                           //(60秒*60分*24小时)秒/天

echo "离五一放假时间还有"." ".$sub1." "."小时!!!";
echo "<p>";
echo "离端午节放假时间还有"." ".$sub2." "."天!!!";

?>
Copy after login

The code running result:

Detailed explanation of functional examples of countdown using PHP time and date function strtotime()

Detailed explanation of functional examples of countdown using PHP time and date function strtotime() is above In the example we used the ceil() function, here we learn about the ceil() function

ceil() function: is Rounded to the nearest integer. The syntax format is as follows:

ceil(x)
Copy after login

Explanation

Returns the next integer that is not less than x. If x has a decimal part, it will be rounded up by one. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.

Look at an example of the ceil() function

<?php

echo ceil(0.60)."<br/>";

echo   ceil(5.1);

?>
Copy after login

Example running result of the ceil() function:

Detailed explanation of functional examples of countdown using PHP time and date function strtotime()

In the next section, we will explain to you how to "calculate the running time of the page script".

The above is the detailed content of Detailed explanation of functional examples of countdown using PHP time and date function strtotime(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!