PHP日期和时间函数的使用示例详解
作者:Monkey王 时间:2023-06-28 07:28:25
(1)查看日期函数拓展
phpinfo()
<?php
phpinfo();
打开上述页面之后,可以看到以下,证明已经安装日期拓展
浏览器输入php.net访问php的文档
(2)设置时区
方式一:修改配置文件php.ini
D:\ITSoft\wamp64\bin\php\php7.0.10\php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC
; http://php.net/date.default-latitude
;date.default_latitude = 31.7667
; http://php.net/date.default-longitude
;date.default_longitude = 35.2333
; http://php.net/date.sunrise-zenith
;date.sunrise_zenith = 90.583333
; http://php.net/date.sunset-zenith
;date.sunset_zenith = 90.583333
方式二:date_default_timezone_set
<?php
echo '当前时区'.date_default_timezone_get(); //UTC
var_dump(date_default_timezone_set('Asia/Shanghai'));//true
echo '当前时区'.date_default_timezone_get(); //Asia/Shanghai
方式三:init_set
设置指定配置选项的值。这个选项会在脚本运行时保持新的值,并在脚本结束时恢复
<?php
echo ini_get('date.timezone'); //UTC
ini_set('date.timezone','Asia/Shanghai');
echo ini_get('date.timezone'); //Asia/Shanghai
(3)常用时间函数
常用参数
date使用
<?php
ini_set('date.timezone','Asia/Shanghai');
echo date('Y年m月d日 H:i:s');//分隔符随意
echo date('H:i:s a');//am pm
echo date('w');//星期
//判断是否闰年
$runNian = date('L');
if($runNian){
echo '是闰年';
}else{
echo '不是闰年';
}
time mktime的使用
<?php
//获取当前时间戳
echo time();//1513757887
//时间戳转换成date
echo date('Y-m-d H:i:s',time());
//一天后的时间
echo date('Y-m-d H:i:s',time()+24*60*60);
//获取指定日期的时间戳
echo mktime(0,0,0,2,1,2016);//(H i s n j Y) 2016-2-1 0:0:0的时间戳 可以依次省略
strtotime使用
<?php
echo date('Y-m-d H:i:s',time());//2017-12-20 08:30:58
echo date('Y-m-d H:i:s',strtotime('+1 day'));//2017-12-21 08:30:58
echo date('Y-m-d H:i:s',strtotime('-5 days'));//2017-12-15 08:30:58
echo date('Y-m-d H:i:s',strtotime('+1 month'));//2017-01-20 08:30:58
echo date('Y-m-d H:i:s',strtotime('+5 months'));//2017-05-20 08:30:58
echo date('Y-m-d H:i:s',strtotime("last Monday"));//2017-12-18 08:30:58
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
microtime()
<?php
echo microtime(); //0.85666200 1513758991 两部分微妙值 时间戳
echo microtime(true);// 1513759077.8005
其他函数
<?php
//获取当前日期
print_r(getdate());
// 输出如下
//Array (
// [seconds] => 53
// [minutes] => 40
// [hours] => 8
// [mday] => 20
// [wday] => 3
// [mon] => 12
// [year] => 2017
// [yday] => 353
// [weekday] => Wednesday
// [month] =>
// December [0] => 1513759253
//)
<?php
//验证日期的合法性
var_dump(checkdate(1,32,2014));//false
(4)时间控件的使用test.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>H5新增表单控件</title>
</head>
<body>
<h1>H5新增表单日期时间控件</h1>
<form action='doAction.php' method='post'>
<input type="date" name="datetime1" id="time1"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
doAction.php
<?php
print_r($_POST);
来源:https://blog.csdn.net/sinat_35615296/article/details/78855611
标签:PHP,日期,时间,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
[机器视觉]使用python自动识别验证码详解
2021-09-12 03:24:36
![](https://img.aspxhome.com/file/2023/1/115861_0s.jpg)
Python中Tkinter Scrollbar滚动条(窗口滑动条)
2021-11-12 00:22:59
![](https://img.aspxhome.com/file/2023/1/103491_0s.jpg)
Java游戏服务器之数据库表存取封装
2024-01-13 13:08:17
python将天气预报可视化
2021-01-19 01:37:46
![](https://img.aspxhome.com/file/2023/5/110495_0s.jpg)
Javascript中常见的逻辑题和解决方法
2024-05-22 10:31:21
python bmp转换为jpg 并删除原图的方法
2023-03-12 02:48:50
用户体验设计何去何从,交互设计师又何去何从?
2009-12-28 13:07:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/2009-09-17_01-34s.png)
ASP函数指针试探-GetRef
2009-10-12 12:39:00
Microsoft SQL Server 2000安装问题集锦
2008-12-10 14:26:00
Python 数据处理库 pandas进阶教程
2022-04-18 01:17:13
![](https://img.aspxhome.com/file/2023/2/97752_0s.png)
Python中itertools的用法详解
2022-06-05 13:34:52
Flask使用Pyecharts在单个页面展示多个图表的方法
2021-10-12 18:16:35
![](https://img.aspxhome.com/file/2023/4/82694_0s.jpg)
发一个较复杂的字符串截取函数
2009-12-08 16:23:00
Tensorflow实现神经网络拟合线性回归
2022-10-09 06:24:49
![](https://img.aspxhome.com/file/2023/4/92514_0s.jpg)
Python 基于win32com客户端实现Excel操作的详细过程
2021-01-18 11:22:47
浅析Python字符串中的r和u的区别
2021-06-25 04:47:57
Python创建SQL数据库流程逐步讲解
2024-01-23 14:29:17
![](https://img.aspxhome.com/file/2023/0/124980_0s.png)
MySQL查询两个日期之间记录的方法
2024-01-21 19:47:40
深入探寻javascript定时器
2024-04-29 13:36:55
python中plot实现即时数据动态显示方法
2021-04-20 22:09:18
![](https://img.aspxhome.com/file/2023/6/100756_0s.gif)