一、时间戳和日期互相转换
// 获取时间戳
$date = time(); // 获取当前时间戳
$date = mktime(0, 0, 0, 10, 10, 2020); // 获取指定时间的时间戳 2020年10月10日0时0分0秒
// 日期转换为时间戳
$date = "2019-08-08 08:08:08";
$timestamp = strtotime($date);
// 将时间戳转换成日期
$date = time();
echo date('Y-m-d', $date); // 输出格式化的日期(年-月-日)
// 将时间戳转换为时间格式
$date = time();
echo date('H:i:s', $date); // 输出格式化的时间(小时:分钟:秒)
// 日期格式化
$date = time();
echo date('Y-m-d H:i:s', $date); // 输出格式化的日期时间(年-月-日 小时:分钟:秒)
// 将时间戳转换为星期
$date = time();
echo date('l', $date); // 输出星期几的完整文本形式(例如:Sunday)
// 将时间戳转换为月份
$date = time();
echo date('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-01 00:00:00',strtotime('-1 month'));
$end_time = date("Y-m-d 23: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");
三、获取当前周的每天的起始时间
function getDay(){
$timestr = time(); //当前时间戳
$now_day = date('w',$timestr); //当前是周几
//获取周一
$monday_str = $timestr - ($now_day-1)*60*60*24;
$monday = date('Y-m-d', $monday_str);
//获取周日
$sunday_str = $timestr + (7-$now_day)*60*60*24;
$sunday = date('Y-m-d', $sunday_str);
for($i=0;$i<7;$i++)
{
$arr[$i]['start']=strtotime(date('Y-m-d',strtotime($monday.'+'.$i.'day')));
$arr[$i]['end']=strtotime(date('Y-m-d',strtotime($monday.'+'.$i.'day')). " 24:00:00");
}
return $arr;
}
四、获取周的起始时间
1、根据指定时间获取所在周的起始时间和结束时间
/**
* @param data 日期
*/
function get_weekinfo_by_time($date) {
$idx = strftime("%u", strtotime($date));
$mon_idx = $idx - 1;
$sun_idx = $idx - 7;
return array(
'week_start_day' => strftime('%Y-%m-%d', strtotime($date) - $mon_idx * 86400),
'week_end_day' => strftime('%Y-%m-%d', strtotime($date) - $sun_idx * 86400),
);
}
2、通过时间戳 获取某周的开始时间和结束时间
/**
* @param time 时间
* @param first 表示每周星期一为开始日期 0表示每周日为开始日期
*/
function getWeekMyActionAndEnd($time = '', $first = 1)
{
//当前日期
if (!$time) $time = time();
$sdefaultDate = date("Y-m-d", $time);
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$w = date('w', strtotime($sdefaultDate));
//获取本周开始日期,如果$w是