时间戳
当前天的时间戳直接使用当前时间格式,指定起始以及结束时间来实现快速拿到时间戳的效果。
$startTime=strtotime(date('Y-m-d').'00:00:00');
$overTime=strtotime(date('Y-m-d').'23:59:59');
日期格式
相应的,咱们可以直接字符串拼接实现日期格式的显示。
//弱类型语言,直接拼接字符串
$startDate=date('Y-m-d').'00:00:00';
$overDate=date('Y-m-d').'00:00:00';
昨天时间戳与日期
时间戳
$startTime=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$overTime=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
日期格式
方法一:根据时间戳转日期格式
//根据上面的时间戳进行格式转换
$startDate=date("Y-m-dH:i:s",$startTime);
$overDate=date("Y-m-dH:i:s",$overTime);
新想法:根据首先获取当前天日期,然后使用date函数进行时间格式转换
//获取当前日期的天数的数值减一之后就是昨天啦
$time=date('d')-1;
$startDate=date("Y-m-".$time."00:00:00",time());
$overDate=date("Y-m-".$time."23:59:59",time());
但是在月初时会出现日期为0的异常,除了进行判断,不知道有没有其他简便的方法可以解决,不然还是时间戳转日期格式比较简便,希望有简单解决办法的大佬给点新想法。
本周时间戳与日期
时间戳
date()函数中date(‘w’)可以获取今天是本周第几天,通过简单处理就可以得到本周的起始以及结束时间。
这种思路和方法可以推广到上周的起始和结束时间。
方法一:
//本周开始时间戳
$startTime=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));
//本周结束时间戳
$overTime=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y'));
方法二:
$nowDate=date("Y-m-d");
$week=date('w',strtotime($nowDate));
$startTime=strtotime("$nowDate-".($week?$week-1:6).'days');//本周第一天
$overTime=$start_time+86400*7-1;//本周最后一天
日期格式
使用日期格式函数转换时间戳,也可以用上面的方法进行date()函数中格式,进行转换。
//本周开始时间戳
$startTime=date("Y-m-dH:i:s",mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y')));
//本周结束时间戳
$overTime=date("Y-m-dH:i:s",mktime(23,59,59,date('m'),date('d')-date('w')+7,date('y')));
本月时间戳和日期
时间戳
//本月起始时间时间戳
$startTime=mktime(0,0,0,date('m'),1,date('Y'));
//本月结束时间时间戳
$overTime=mktime(23,59,59,date('m'),date('t'),date('Y'));
日期格式
使用date()函数进行时间戳转换日期格式。
//本月起始时间日期格式
$startTime=date("Y-m-dH:i:s",mktime(0,0,0,date('m'),1,date('Y')));
//本月结束时间日期格式
$overTime=date("Y-m-dH:i:s",mktime(23,59,59,date('m'),date('t'),date('Y')));
本季度时间戳和日期
时间戳
//获取当前季度
$season=ceil((date('m'))/3);
//当前季度开始时间戳
$startTime=mktime(00,00,00,$season*2+1,1,date('Y'));
//获取当前季度结束时间戳
$overTime=mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y'));
日期格式
把上面的时间戳转换为日期格式
date("Y-m-d",$startTime)
date("Y-m-d",$overTime)
当前年时间戳和日期
时间戳
//本年开始
$startTime=strtotime(date("Y",time())."-1"."-1");
//本年结束
$overTime=strtotime(date("Y",time())."-12"."-31");
日期格式
//本年开始
$startTime=date("Y-m-dH:i:s",strtotime(date("Y",time())."-1"."-1"));
//本年结束
$overTime=date("Y-m-dH:i:s",strtotime(date("Y",time())."-12"."-31"));
strtotime函数获取描述对应时间
明天当前时间
strtotime("+1day")//时间戳
date("Y-m-dH:i:s",strtotime("+1day"))//日期格式
昨天当前时间
strtotime("-1day")//时间戳
date("Y-m-dH:i:s",strtotime("-1day"))//日期格式
下个星期当前时间
strtotime("+1week")//时间戳
date("Y-m-dH:i:s",strtotime("+1week"))//日期格式
上个星期当前时间
strtotime("-1week")//时间戳
date("Y-m-dH:i:s",strtotime("-1week"))//日期格式
下星期几当前时间
strtotime("nextThursday")//时间戳
date("Y-m-dH:i:s",strtotime("nextThursday"))//日期格式
上星期几当前时间
strtotime("lastThursday")//时间戳
date("Y-m-dH:i:s",strtotime("lastThursday"))//日期格式
时间戳转日期格式
$timestamp=1664170375;//当前时间戳
date("Y-m-dH:i:s",$timestamp);//转换为日期格式
日期格式转时间戳
$time='2022-09-2623:31:59';//时间格式参数
strtotime($time);//转换为时间戳
获取特定时间戳函数
/**特定时间戳函数
*@param$targetTime
*/
functiongettimestamp($targetTime){
switch($targetTime){
case'today'://今天
$timeamp['start']=strtotime(date('Y-m-d'));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('+1day')));
break;
case'yesterday'://昨天
$timeamp['start']=strtotime(date('Y-m-d',strtotime('-1day')));
$timeamp['over']=strtotime(date('Y-m-d'));
break;
case'beforyesterday'://前天
$timeamp['start']=strtotime(date('Y-m-d',strtotime('-2day')));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('-1day')));
break;
case'beforweek'://本周
$timeamp['start']=strtotime(date("Y-m-dH:i:s",mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"))));
$timeamp['over']=strtotime(date("Y-m-dH:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))));
break;
case'nowmonth'://本月
$timeamp['start']=strtotime(date('Y-m-01'));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('+1day')));
break;
case'permonth'://上月
$timeamp['start']=strtotime(date('Y-m-01',strtotime('-1month')));
$timeamp['over']=strtotime(date('Y-m-01'));
break;
case'preweek'://上周注意我们是从周一开始算
$timeamp['start']=strtotime(date('Y-m-d',strtotime('-2weekMonday')));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('-1weekMonday+1day')));
break;
case'nowweek'://本周
$timeamp['start']=strtotime(date('Y-m-d',strtotime('-1weekMonday')));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('+1day')));
break;
case'preday'://30
$timeamp['start']=strtotime(date('Y-m-d'),strtotime($param.'day'));
$timeamp['end']=strtotime(date('Y-m-d'));
break;
case'nextday'://30
$timeamp['start']=strtotime(date('Y-m-d'));
$timeamp['over']=strtotime(date('Y-m-d'),strtotime($param.'day'));
break;
case'preyear'://去年
$timeamp['start']=strtotime(date('Y-01-01',strtotime('-1year')));
$timeamp['over']=strtotime(date('Y-12-31',strtotime('-1year')));
break;
case'nowyear'://今年
$timeamp['start']=strtotime(date('Y-01-01'));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('+1day')));
break;
case'quarter'://季度
$quarter=ceil((date('m'))/3);
$timeamp['start']=mktime(0,0,0,$quarter*3-2,1,date('Y'));
$timeamp['over']=mktime(0,0,0,$quarter*3+1,1,date('Y'));
break;
default:
$timeamp['start']=strtotime(date('Y-m-d'));
$timeamp['over']=strtotime(date('Y-m-d',strtotime('+1day')));
break;
}
return$timeamp;
}
|