php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
                                  php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime。下面是使用 mktime 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法:
mktime用法:
mktime(hour, minute, second, month, day, year, is_dst)
 
参数介绍:
                                
                                                            
                            
                        <?php
date_default_timezone_set('PRC');
//php获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
//php获取上周起始时间戳和结束时间戳
$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
//php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
// 输出
echo sprintf("今日: %s -> %s\n", $beginToday, $endToday);
echo sprintf("    %s -> %s\n", date("Y-m-d H:i:s", $beginToday), date("Y-m-d H:i:s", $endToday));
echo sprintf("昨日: %s -> %s\n", $beginYesterday, $endYesterday);
echo sprintf("    %s -> %s\n", date("Y-m-d H:i:s", $beginYesterday), date("Y-m-d H:i:s", $endYesterday));
echo sprintf("上周: %s -> %s\n", $beginLastweek, $endLastweek);
echo sprintf("    %s -> %s\n", date("Y-m-d H:i:s", $beginLastweek), date("Y-m-d H:i:s", $endLastweek));
echo sprintf("本月: %s -> %s\n", $beginThismonth, $endThismonth);
echo sprintf("    %s -> %s\n", date("Y-m-d H:i:s", $beginThismonth), date("Y-m-d H:i:s", $endThismonth));输出结果:今日: 1476633600 -> 1476719999 2016-10-17 00:00:00 -> 2016-10-17 23:59:59 昨日: 1476547200 -> 1476633599 2016-10-16 00:00:00 -> 2016-10-16 23:59:59 上周: 1476028800 -> 1476633599 2016-10-10 00:00:00 -> 2016-10-16 23:59:59 本月: 1475251200 -> 1477929599 2016-10-01 00:00:00 -> 2016-10-31 23:59:59
mktime用法:
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,自 PHP 5.1.0 起,is_dst 参数被废弃。
3 个评论
                                            啊啊啊                                        
                                    
                                            休息休息                                        
                                    
                                            xxxxxxxxxxx                                        
                                    