1. 程式人生 > 其它 >PHP輸出一年每週開始結束時間,輸出每月開始結束時間

PHP輸出一年每週開始結束時間,輸出每月開始結束時間

技術標籤:PHP每週開始時間每月開始時間

PHP輸出一年每週開始結束時間,輸出每月開始結束時間

<?php
//輸出每週開始結束時間
function week_time($year){
    $weeks = date("W", mktime(0, 0, 0, 12, 28, $year));
    echo $year . '年一共有' . $weeks . '周<br />';
    for($week=1;$week<$weeks;$week++){
        if ($week > $weeks || $week <= 0)
        {
         $week = 1;
        }
        if ($week < 10)
        {
         $week = '0' . $week;
        }
        $timestamp[$week]['start'] = date('Y-m-d H:i:s',strtotime($year . 'W' . $week));
        $timestamp[$week]['end'] = date('Y-m-d 23:59:59',strtotime('+1 week -1 day', strtotime($year . 'W' . $week)));
    }
    return $timestamp;
}
//輸出每月開始結束時間
function month_time($year){
	$yeararr = [];
	$month = [];
	for ($i=1; $i <=12 ; $i++) { 
		$yeararr[$i] = $year.'-'.$i;
	}
	foreach ($yeararr as $key => $value) {
		$timestamp = strtotime( $value );
		$start_time = date( 'Y-m-1 00:00:00', $timestamp );
		$mdays = date( 't', $timestamp );
		$end_time = date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp );

		$month[$key]['start_time'] = $start_time;
		$month[$key]['end_time'] = $end_time;

	}
	return $month;
}


header("Content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$year = 2020;
//周
print_r(week_time($year));
//月
print_r(month_time($year));

結果如下

每週開始結束時間
每週開始結束時間
每月開始結束時間

歡迎關注我的公眾號"零零糖",不要總是閱讀雞湯文。