PHP获取当前时间、时间戳及互相转换函数用法
date("Y-m-d H:i:s"); // 2017-12-14 23:13:51
2.获取当前时间戳:
time(); // 1513264258
3.获取某个时间戳对应的日期字符串:
date("Y-m-d H:i:s",1513264258); // 2017-12-14 23:13:51
4.获取某个日期的时间戳
strtotime('2017-12-14 23:13:51'); // 1513264258
5.获取的时间与当前时间相差6小时解决方式
这是由于时区设置问题,只要将之设为重庆时间即可。方式如下:
1.在php.ini中找到date.timezone,将它的值改成Asia/Shanghai,即date.timezone=Asia/Shanghai
2.在程序开始时添加date_default_timezone_set(‘Asia/Shanghai’)即可。
date()、time()、strtotime()函数的具体使用方式
句型:date()函数
date(format,timestamp);
定义和用法
date()函数带有两个参数,第一个是格式字符串,第二个(可选)是UNIX时间戳。若果没有指定时间戳oracle unix时间戳转换成时间,在默认的情况下,date()函数将返回当前的日期和时间。有则返回时间戳对应的低格日期字符串。
事例:
<?php
header("Content-type: text/html; charset=utf-8");
//将时区设置为上海时间
date_default_timezone_set('Asia/Shanghai');
echo date("Y-m-d H:i:s");
句型:time()函数
time();
定义和用法
time()函数返回自Unix纪元(January1197000:00:00GMT)起的当前时间的秒数(时间戳)。
句型:strtotime()函数
strtotime(time,now);
定义和用法
strtotime()函数将任何中文文本的日期或时间描述解析为Unix时间戳(自January1197000:00:00GMT起的秒数)。
注意:假如年份表示使用两位数格式ubuntu linux,则值0-69会映射为2000-2069,值70-100会映射为1970-2000。
注意:请注意m/d/y或d-m-y格式的日期,假如分隔符是斜线(/),则使用欧洲的m/d/y格式。假如分隔符是横杠(-)或则点(.)oracle unix时间戳转换成时间linux系统安装,则使用法国的d-m-y格式。为了防止潜在的错误,您应当尽可能使用YYYY-MM-DD格式或则使用date_create_from_format()函数。
事例:
<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set('Asia/Shanghai');
echo(strtotime("now") . "
");
echo(strtotime("15 October 2019") . "
");
echo(strtotime("+5 hours") . "
");
echo(strtotime("+1 week") . "
");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "
");
echo(strtotime("next Monday") . "
");
echo(strtotime("last Sunday"));
echo(strtotime("2011-11-11 11:11:11"));
如下字符表示date()函数的规定输出日期字符串的格式:
同时,也可使用下述预定义常量(从PHP5.1.0开始可用):