天天看点

PHP mktime函数获取今天的开始和结束时间戳

php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime()。

mktime函数用法如下:mktime(hour,minute,second,month,day,year,is_dst)

参数描述

hour  可选。规定小时。

minute 可选。规定分钟。

second 可选。规定秒。

month  可选。规定用数字表示的月。

day   可选。规定天。

year  可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。

1  //获取今日开始时间戳和结束时间戳
 2  $start = mktime(0,0,0,date('m'),date('d'),date('Y'));
 3  $end = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
 4  //获取昨日起始时间戳和结束时间戳
 5  $beginYesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
 6  $endYesterday = mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
 7  //获取上周起始时间戳和结束时间戳
 8  $beginLastweek = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
 9  $endLastweek = mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
10  //获取本月起始时间戳和结束时间戳
11  $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
12  $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));