Home > Backend Development > PHP Tutorial > PHP time comparison and time difference calculation implementation code

PHP time comparison and time difference calculation implementation code

小云云
Release: 2023-03-19 20:10:02
Original
1464 people have browsed it

This article mainly introduces the method of PHP to implement time comparison and time difference calculation, involving PHP date and time conversion, calculation and other related operation skills. Friends who need it can refer to it. I hope it can help everyone.

Example 1:


##

<?php
//PHP时间比较和时间差计算:
//(1).比较两个绝对时间的大小
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set(&#39;PRC&#39;);
$zero1=date("Y-m-d h:i:s");
//$zero1="2010-11-29 21:07:00";
$zero2="2010-11-29 21:07:00";
echo "zero1的时间为:".$zero1."<br>";
echo "zero2的时间为:".$zero2."<br>";
// strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳
if(strtotime($zero1)<strtotime($zero2)){
 echo "zero1早于zero2";
}else if(strtotime($zero1)>strtotime($zero2)){
 echo "zero2早于zero1";
}else{
 echo "zero2等于zero1";
}
echo "<br/><br/>";
?>
Copy after login

Running result:


zero1的时间为:2017-07-24 12:18:39
zero2的时间为:2010-11-29 21:07:00
zero2早于zero1
Copy after login

Example 2:


<?php
//(2).倒计时小程序
$zero1=strtotime (date("y-m-d h:i:s")); //当前时间 ,注意H 是24小时 h是12小时
$zero2=strtotime ("2018-1-1 00:00:00"); //过年时间
//float ceil ( float $value )
//返回不小于 value 的下一个整数,value 如果有小数部分则进一位。
$guonian=ceil(($zero2-$zero1)/86400); //60s*60min*24h
echo "离过年还有<strong>$guonian</strong>天!";
echo "<br/><br/>";
?>
Copy after login

Running result:


离过年还有161天
Copy after login

Example 3:


<?php
//(3).PHP计算两个时间差的方法
$startdate=date("y-m-d H:i:s");
$enddate="2017-7-30 18:00:00";
// floor — 舍去法取整
// float floor ( float $value )
// 返回不大于 value 的最接近的整数,舍去小数部分取整。
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
$hour=floor((strtotime($enddate)-strtotime($startdate))%86400/3600);
$minute=floor((strtotime($enddate)-strtotime($startdate))%86400/60);
$second=floor((strtotime($enddate)-strtotime($startdate))%86400%60);
echo "现在距结束时间还有".$date."天".$hour."小时".$minute."分钟".$second."秒";
echo "<br/><br/>";
?>
Copy after login

Running results:


现在距结束时间还有6天5小时339分钟56秒
Copy after login

Related recommendations:


php Comparison of running time of four basic sorting algorithms

php time comparison function, returns the number of seconds, minutes, hours or days between two dates

Three functions for date and time comparison in php_PHP tutorial

The above is the detailed content of PHP time comparison and time difference calculation implementation code. 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