PHP中比较两个时间的大小与日期的差值
在这里我们全用到时间戳
mktime(hour,minute,second,month,day,year,[is_dst])
其参数可以从右向左省略,任何省略的参数都会被设置成本地日期和时间的当前值。
参数 描述
hour 可选。规定小时。
minute 可选。规定分钟。
second 可选。规定秒。
month 可选。规定用数字表示的月。
day 可选。规定天。
year 可选。规定年。在某些系统上,合法值介于 1901 – 2038 之间。不过在 php教程 5 中已经不存在这个限制了。
is_dst 可选。如果时间在日光节约时间(dst)期间,则设置为1,否则设置为0,若未知,则设置为-1。自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性
在日常生活中我们要经常比较时间的早晚,对于我们来说判断时间的大小很简单。但是时间的比较不只是单纯的数字大小的比较,因此相对来说还是比较复杂。那么在php中通过什么方式来比较两个时间的大小呢?
要比较两个时间的大小,我们需要将时间转化为时间戳格式,然后再进行比较这是最常用的方法。常用到的函数是:strtotime()
语法格式:strtotime(time,now)
如果time是绝对时间,则now参数不起作用
如果time是相对时间,则相对应的参数则对应函数就是now来提供,如果没有提供now参数,那么相对应的时间就是当前的本地时间。
实例:比较两个绝对时间的大小
代码:
$zero1=date(“y-m-d h:i:s”);
$zero2=”2010-11-29 21:07:00′;
echo “zero1的时间为:”.$zero1.”
”;
echo “zero2的时间为:”.$zero2.”
”;
if(strtotime($zero1)echo “zero1早于zero2′; ($zero2)){
}else{
echo “zero2早于zero1′;
}
?>
输出结果:
zero1的时间为:2010-11-30 21:12:55
zero2的时间为:2010-11-29 21:07:00
zero2早于zero1
注:可以根据实例发散思维
计算两个日期的差值
奥运会倒计时,亚运会倒计时,生日倒计时这些倒计时都可以通过计算两个日期的差值来实现,同样需要用到strottime()函数。
实现倒计时需要将两个时间的差值整数化,需要用到函数ceil()
ceil()函数的作用是求不小于给定实数的最小整数
实例:倒计时小程序
实例代码:
$zero1=strtotime (date(“y-m-d h:i:s”)); //当前时间
$zero2=strtotime (“2011-2-03 24:00:00′); //过年时间
$guonian=ceil(($zero2-$zero1)/86400); //60s*60min*24h
echo “离过年还有$guonian天!”;
?>
输出结果:
离过年还有66天!
strtotime()函数解析
定义和用法
strtotime() 函数将任何英文文本的日期时间描述解析为 unix 时间戳。
语法
strtotime(time,now)参数 描述
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。
说明
该函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 unix 时间戳(自 january 1 1970 00:00:00 gmt 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数,则用系统当前时间

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
