基本信息
文件名称:PHP获取时间戳等相关函数汇总.docx
文件大小:19.71 KB
总页数:12 页
更新时间:2025-05-21
总字数:约8.72千字
文档摘要

PHP获取时间戳等相关函数汇总

目录一、时间戳和日期互相转换二、PHP获取今日、昨日、上周、本周、上月、本月的起始时间戳三、获取当前周的每天的起始时间四、获取周的起始时间1、根据指定时间获取所在周的起始时间和结束时间2、通过时间戳获取某周的开始时间和结束时间五、获取指定日期是周几六、通过某个日期段内的周几获取对应的日期开始日期结束日期七、获取指定日期之间的各个周八、获取指定日期之间的各个月九、根据指定日期获取所在月的起始时间和结束时间十、获取指定年份的每个月的起始时间十一、获取指定月份的起止时间戳

一、时间戳和日期互相转换

//获取时间戳

$date=time();//获取当前时间戳

$date=mktime(0,0,0,10,10,2025);//获取指定时间的时间戳2025年10月10日0时0分0秒

//日期转换为时间戳

$date=2025-08-0808:08:08;

$timestamp=strtotime($date);

//将时间戳转换成日期

$date=time();

echodate(Y-m-d,$date);//输出格式化的日期(年-月-日)

//将时间戳转换为时间格式

$date=time();

echodate(H:i:s,$date);//输出格式化的时间(小时:分钟:秒)

//日期格式化

$date=time();

echodate(Y-m-dH:i:s,$date);//输出格式化的日期时间(年-月-日小时:分钟:秒)

//将时间戳转换为星期

$date=time();

echodate(l,$date);//输出星期几的完整文本形式(例如:Sunday)

//将时间戳转换为月份

$date=time();

echodate(F,$date);//输出月份的完整文本形式(例如:January)

二、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;

//昨日起始时间戳和结束时间戳

$beginYesterday=mktime(0,0,0,date(m),date(d)-1,date(Y));

$endYesterday=mktime(0,0,0,date(m),date(d),date(Y))-1;

//本周起始时间戳和结束时间戳

$startTime=mktime(0,0,0,date(m),date(d)-date(w)+1,date(y));

$endTime=mktime(23,59,59,date(m),date(d)-date(w)+7,date(y));

//上周起始时间戳和结束时间戳

$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));

//本月起始时间戳和结束时间戳

$beginThismonth=mktime(0,0,0,date(m),1,date(Y));

$endThismonth=mktime(23,59,59,date(m),date(t),date(Y));

//上月起始时间戳和结束时间戳

$begin_time=date(Y-m-0100:00:00,strtotime(-1month));

$end_time=date(Y-m-d23:59:59,strtotime(-date(d).day));

//获取当前季度

$season=ceil((date(m))/3);

//本季度起始时间戳和结束时间戳

$starTime=mktime(0,0,0,$season*3-3+1,1,date(Y));

$endTime=mktime(23,59,59,$season*3,date(t,mktime(0,0,0,$season*3,1,date(Y))),date(Y));

//当年起始时间戳和结束时间戳

$startTime=strtotime(date(Y,time()).-1.-1);

$overTime=strtotime(date(Y,time()).-12.-31);

三、获取当