开胜科技-网站建设专家

售前咨询:点击这里给我发消息 点击这里给我发消息 

技术支持:点击这里给我发消息 点击这里给我发消息 

售后服务:点击这里给我发消息 点击这里给我发消息 

电话:0531-89004900

手机:18660105139

E-mail:service@openwin.cn

网络推广

PHP获取昨天,本周,本月,今年范围的时间戳

发布时间:2016-06-05 18:18:27      浏览次数:0

 以下代码为php获取昨天、本周、本月的时间戳,需要的童鞋可以收藏以备后用!
直接上代码。

昨天  
$t=time();
$start=mktime(0,0,0,date("m",$t),date("d",$t)-1,date("Y",$t));
$end=mktime(23,59,59,date("m",$t),date("d",$t)-1,date("Y",$t));


今年
$pay = 0;
//求得年份
$year = @date("Y",time());
//一年有多少天
$days = ($year % 4 == 0 && $year % 100 != 0 || $year % 400 == 0) ? 366 : 365;
//今年第一天的时间戳
$first = strtotime("$year-01-01");
//今年最后一天的时间戳
$last = strtotime("+ $days days", $first);


本周
$ftime = mktime ( 0, 0, 0, date ( "m" ), date ( "d" ) - date ( "w" ) + 1, date ( "Y" ) );
$ltime = mktime ( 23, 59, 59, date ( "m" ), date ( "d" ) - date ( "w" ) + 7, date ( "Y" ) );
//下面这个不太准
$year = date("Y");
$month = date("m");
$day = date('w');
$nowMonthDay = date("t");
 
$firstday = date('d') - $day;
if(substr($firstday,0,1) == "-"){
$firstMonth = $month - 1;
$lastMonthDay = date("t",$firstMonth);
$firstday = $lastMonthDay - substr($firstday,1);
$time_1 = strtotime($year."-".$firstMonth."-".$firstday);
}else{
$time_1 = strtotime($year."-".$month."-".$firstday);
}
 
$lastday = date('d') + (7 - $day);
if($lastday > $nowMonthDay){
$lastday = $lastday - $nowMonthDay;
$lastMonth = $month + 1;
$time_2 = strtotime($year."-".$lastMonth."-".$lastday);
}else{
$time_2 = strtotime($year."-".$month."-".$lastday);
}
$time_1 += 86400;
$time_2 += 86400;

本月
$year = date("Y");
$month = date("m");
$allday = date("t");
$start = strtotime($year."-".$month."-1");
$end = strtotime($year."-".$month."-".$allday);

上周
echo '<br>上周:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")))," ";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")))," ";
echo '<br>本周:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")))," ";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")))," ";
 
echo '<br>上月:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")))," ";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")))," ";
echo '<br>本月:<br>';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")))," ";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")))," ";
 
$getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));//本季度未最后一月天数
echo '<br>本季度:<br>';
echo date('Y-m-d H:i:s', mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y')))," ";
echo date('Y-m-d H:i:s', mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y')))," ";
 

 
开胜科技微信(openwin-cn)