php 获取今日、昨日、上周、本周、本月、上月、今年、最近一个月的起始时间戳和结束时间戳的方法

 

 

复制代码
//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;
//
$beginToday=strtotime(date('Y-m-d',strtotime('0 day')));
$endToday=strtotime(date('Y-m-d',strtotime('+1 day')));
//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;
//
$beginYesterday=strtotime(date('Y-m-d',strtotime('-1 day')));
$endYesterday=strtotime(date('Y-m-d',strtotime('0 day')));
//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获取上周起始时间戳和结束时间戳【正确写法】
$beginLastweek=strtotime("last week Monday",time());
$endLastweek=strtotime("last week Sunday",time())+86399;
//php获取本周起始时间戳和结束时间戳【开始时间周一,结束时间为周日】
$beginLastweek=strtotime("this week Monday",time());
$endLastweek=strtotime("this week Sunday",time())+86399;
//php获取本周起始时间戳和结束时间戳【开始时间周日,结束时间为周六】
$beginLastweek=mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
$endLastweek=mktime(0, 0 , 0,date("m"),date("d")-date("w")+7,date("Y"));
//php获取上一月起始时间戳和结束时间戳
$beginLastmonth=mktime(0, 0 , 0,date("m")-1,1,date("Y"));
$endLastmonth=mktime(23,59,59,date("m") ,0,date("Y"));
//php获取上上月起始时间戳和结束时间戳
$beginLastmonth2=mktime(0, 0 , 0,date("m")-2,1,date("Y"));
$endLastmonth2=mktime(23,59,59,date("m")-1 ,0,date("Y"));
//php获取本月起始时间戳和结束时间戳
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
//php获取今年起始时间戳和结束时间戳
$beginThismonth=strtotime(date("Y",time())."-1"."-1");
$endThismonth=strtotime(date("Y",time())."-12"."-31");
复制代码

 

 

strtotime(date("Y-m-d", strtotime("-1 month")));        //最近一个月,最近30天
strtotime(date('Y-m-d', strtotime('-29 day')))
 

 

PHP mktime() 函数用于返回一个日期的 Unix 时间戳。

语法

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。

自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。

 

用法

参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。

参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。

注意在 PHP 5.1 之前,如果该函数的参数非法,则会返回 false。

另外需要注意的是该函数对于日期运算和验证非常有用。它可以自动校正越界的输入,如:

 
echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));

 

将输出结果如:

Jan-05-2002

posted @   一个人的孤独自白  阅读(994)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
历史上的今天:
2017-01-17 mysql判断两个时间段是否有交集
2017-01-17 多维数组进行排序
点击右上角即可分享
微信分享提示