php的日期时间函数
来源:互联网 发布:morphvoxpro萌妹子数据 编辑:程序博客网 时间:2024/05/16 01:30
时间戳
时间戳我就不赘述了,手册里有,就是能精确的表示一个时间点。我在做项目的时候经常用时间戳来表示数据,这样比较方便,如果保存为日期时间型的数据,显示的时候可能比较省事,但是如果是获取这个日期的某个年份或月份,就比较麻烦了。<?phpecho time();//返回一个以秒为单位的时间戳 :1467968841echo microtime(true);//返回一个以毫秒为单位的时间戳 :1467968841.4155echo microtime();//返回带毫秒和秒的时间戳 :0.41553000 1467968841,前面是毫秒小数,后面是秒数?>
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
时间戳格式化
1、date($format,$timestamp):在当前使用的时区下格式化时间戳
2、gmdate($format,$timestamp):指定UTC时区下格式化时间戳,不论用户使用什么时区时区的概念:时区是按照地点设置的时间,中国属于东8区,所以比UTC(格林尼治时间)要快8个小时
获取和设置时区
date_default_timezone_get()//获得当前使用的时区,默认情况下应该是UTC
date_default_timezone_set(‘Asia/Shanghai’)//设置当前的时区为亚洲上海
<?phpecho date("Y-m-d 星期N H:i:s a",$time);//2016-07-08 星期5 10:44:26 am(默认时区下)echo date("Y-m-d 星期N H:i:s a",0);//1970-01-01 星期4 00:00:00(默认时区下)echo gmdate("Y-m-d 星期N H:i:s a",0);//1970-01-01 星期4 00:00:00(全部在UTC时区下)
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
?>
解析时间格式变为时间戳
1、mktime():将制定的时间格式解析成时间戳<?phpecho mktime(19,15,0,7,8,2016);//获取2016年8月7日19时15分0秒的时间戳?>
- 1
- 2
- 3
- 1
- 2
- 3
2、strtotime():将任何英文文本的日期时间描述解析为时间戳
<?php echo strtotime("2016-7-8 19:15:0");//获取2016年8月7日19时15分0秒的时间戳 echo strtotime("now");//获取现在得时间戳 echo strtotime("+1 day");//当前时间往后推一天的时间戳 echo strtotime("-1 day");//当前时间往前推一天的时间戳 ?>
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
3、checkdate():检测输入的日期是否存在
<?phpvar_dump(checkdate(2,29,2015));//2015年2月29日不存在,所以是falsevar_dump(checkdate(2,29,2016));//2016年2月29日存在,所以是true?>
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
案例:计算两个日期的日期差数
1、用时间戳来计算
<?php$day1 = "2016-7-8";$day2 = "2016-7-1";echo (strtotime($day1) - strtotime($day2))/(24 * 60 * 60);?>
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
2、用DateTime类来计算
<?php$datetime1 = new DateTime('2016-7-8');$datetime2 = new DateTime('2016-7-1');$interval = $datetime2->diff($datetime1);echo $interval->format('%R%a days');?>
0 0
- php的日期时间函数
- php的日期时间函数
- php日期时间函数
- PHP日期时间函数
- PHP 日期时间函数
- PHP的日期时间函数date()
- PHP的日期时间函数date()
- PHP的日期时间函数date()
- PHP的日期时间函数date()
- 详细讲解PHP的日期时间函数
- PHP的日期时间函数date()
- PHP的日期时间函数date()
- PHP日期与时间相关的函数
- PHP的日期时间函数date()
- 详解PHP的日期时间函数date()
- 详解PHP的日期时间函数date
- [Lpp]PHP的日期/时间函数
- PHP 日期与时间函数
- oracle11g文件目录结构图
- ID3Tag Intro - Website
- 关于Socket和IOCP的一些值得注意的地方
- AFN拦截重定向设置httpBody
- javascript-BOM和DOM
- php的日期时间函数
- Docker镜像原理详解
- Xcode进行发包时异常(unable to execute command:Segmentation fault:11)
- HTML语法大全
- 矩阵相乘
- word中在分页符后设置标题的段前间距
- snprintf是否会自动将字符串末尾填'\0'?
- PHP命令执行php文件需要注意的问题
- HTML 头信息(ContentType)大全