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." "."天!!!"; ?>
The code running result:
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)
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); ?>
Example running result of the ceil() function:
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!