php根据当前时间获取周、月、季度、年的开始和结束时间

// 获取周的开始结束时间
public function actionWeek ()
{
    $time = time();
    $startTime = strtotime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m", $time),date("d", $time)-date("w", $time)+1-7,date("Y", $time))));
    $endTime = strtotime(date("Y-m-d H:i:s",mktime(23,59,59,date("m", $time),date("d", $time)-date("w", $time)+7-7,date("Y", $time))));
}

// 获取月的开始结束时间
public function actionMonth ()
{
    $time = time();
    $startTime = mktime(0,0,0,date("m",$time)-1,1,date("Y",$time));
    $endTime = mktime(23,59,59,date("m",$time)-1,date("t",$startTime),date("Y",$time));
}

// 获取季度的开始结束时间
public function actionQuarter ()
{
    $time = time();
    $season = ceil((date('n', $time))/3) - 1;//当月是第几季度
    $startTime = strtotime(date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))));
    $endTime = strtotime(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'))));
}

// 获取年的开始结束时间
public function actionYear ()
{
    $date = date('Y-m-d H:i:s');
    $startTime = strtotime(date(date("Y-01-01 00:00:00",strtotime("$date -1 year"))));
    $endTime = strtotime(date('Y-12-31 23:59:59', strtotime("$date -1 year")));
}

你可能感兴趣的:(php)