各种获取日期方法
var_dump(date('Y-m-d H:i:s'));
var_dump(date('w'));
var_dump(date('Y-m-d',strtotime('-1 day',time())));
var_dump(date('Y-m-d',strtotime('-1 week',time())));
var_dump(date('Y-m-d',strtotime('-1 month',time())));
var_dump(date('Y-m-d',strtotime('-1 year',time())));
echo "当天:".date("Y-m-d",strtotime("now")), "\n";
echo "本周星期一日期:".date("Y-m-d",strtotime("-1 week Monday")). "\n";
echo "上周星期天日期:".date("Y-m-d",strtotime("-1 week Sunday")). "\n";
echo "下周周星期一日期:".date("Y-m-d",strtotime("+0 week Monday")). "\n";
echo "本周星期天日期:".date("Y-m-d",strtotime("+0 week Sunday")). "\n";
$time = date("Y-m-d",strtotime("-1 week last monday"));
echo "上周一日期:".($time). "\n";;
$time = date("Y-m-d",strtotime("0 week last monday"));
echo "本周一日期:".($time). "\n";;
$time = date("Y-m-d",strtotime("1 week last monday"));
echo "周一日期:".($time). "\n";;
echo "
";
echo "*********第几个月:" ."\n";
echo date('n') ."\n";
echo "*********本周周几:0(星期天) 1(星期一)" ."\n";
echo date("w") ."\n";
echo "*********本月天数:" ."\n";
echo date("t") ."\n";
echo "*********" ."\n";
echo '
上周起始时间:
';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
echo '
本周起始时间:
';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";
$dayNumber = date('W') * 7;
$weekDayNumber = date("w", mktime(0, 0, 0, 1, $dayNumber, date("Y")));
$startNumber = $dayNumber - $weekDayNumber;
echo date("Y-m-d", mktime(0, 0, 0, 1, $startNumber + 1, date("Y")));
echo date("Y-m-d", mktime(0, 0, 0, 1, $startNumber + 7, date("Y")));
echo '
上月起始时间:
';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
echo '
本月起始时间:
';
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";
$season = ceil((date('n'))/3);
echo '
本季度起始时间:
';
echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";
$season = ceil((date('n'))/3)-1;
echo '
上季度起始时间:
';
echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";
汇总方法
function get_week($date){
$date_str=date('Y-m-d',strtotime($date));
$arr=explode("-", $date_str);
$year=$arr[0];
$month=sprintf('%02d',$arr[1]);
$day=sprintf('%02d',$arr[2]);
$hour = $minute = $second = 0;
$strap = mktime($hour,$minute,$second,$month,$day,$year);
$number_wk=date("w",$strap);
$weekArr=array("周日","周一","周二","周三","周四","周五","周六");
return $weekArr[$number_wk];
}
function get_week_arr($time=0, $format = "Y-m-d"){
if(!$time){
$time = time();
}
$week = date('w', $time);
$weekname = array('星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日');
if (empty($week)) {
$week = 7;
}
for ($i = 0; $i <= 6; $i++) {
$currDay = $i+1-$week;
$data[$i]['date'] = date($format, strtotime("+{$currDay} days", $time));
$data[$i]['week'] = $weekname[$i];
}
return $data;
}
function getWeekBeginEnd($time = '', $first = 1){
if(!$time){
$time = time();
}
$sdefaultDate = date("Y-m-d", $time);
$w = date('w', strtotime($sdefaultDate));
$days = $w ? $w - $first : 6;
$week_start = date('Y-m-d', strtotime("{$sdefaultDate} -{$days} days"));
$week_end = date('Y-m-d', strtotime("{$week_start} +6 days"));
return array("week_start" => $week_start, "week_end" => $week_end);
}