PHP 时间

获取本周每天起始时间戳

$timestamp = time();
        $data['monday'] = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))); //周一
        $data['date_monday'] = date('Y-m-d H:i:s',$data['monday']); //周一
        $data['tuesday'] = strtotime(date('Y-m-d', strtotime("this week Tuesday", $timestamp))); //周二
        $data['date_tuesday'] = date('Y-m-d H:i:s',$data['tuesday']); //周二
        $data['wednesday'] = strtotime(date('Y-m-d', strtotime("this week Wednesday", $timestamp))); //周三
        $data['date_wednesday'] = date('Y-m-d H:i:s',$data['wednesday']); //周三
        $data['thursday'] = strtotime(date('Y-m-d', strtotime("this week Thursday", $timestamp))); //周四
        $data['friday'] = strtotime(date('Y-m-d', strtotime("this week Friday", $timestamp))); //周五
        $data['saturday'] = strtotime(date('Y-m-d', strtotime("this week Saturday", $timestamp))); //周六
        $data['sunday'] = strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))); //周日
        return $data;

获取一年中第几周的开始和结束时间

/*
     * 获取第几周的开始日期和结束日期
     * @param $year  年
     * @param $week  周
     * return  array
     */
    public function weekday($year,$week=1)
    {
        $year_start = mktime(0, 0, 0, 1, 1, $year);
        $year_end = mktime(0, 0, 0, 12, 31, $year);

        // 判断第一天是否为第一周的开始
        if (intval(date('W', $year_start)) === 1) {
            $start = $year_start;//把第一天做为第一周的开始
        } else {
            $week++;
            $start = strtotime('+1 monday', $year_start);//把第一个周一作为开始
        }

        // 第几周的开始时间
        if ($week === 1) {
            $weekday['start'] = $start;
        } else {
            $weekday['start'] = strtotime('+' . ($week - 0) . ' monday', $start);
        }

        // 第几周的结束时间
        $weekday['end'] = strtotime('+1 sunday', $weekday['start']);
        if (date('Y', $weekday['end']) != $year) {
            $weekday['end'] = $year_end;
        }
        return $weekday;
    }

获取月的第一天 0 时 和最后一天23:59:59

/**
 *获取月的第一天 0时 和最后一天 23:59:59 时间
 * @param $year int  年  例:2019
 * @param $month  int  月  例:11
 * return  array
 */
function month_time($year,$month){

    $t = date("t",strtotime("$year-$month"));
    if(empty($year)) $year = date('Y');
    if(empty($month)) $month = date('m');
    $monthFirst = strtotime($year.'-'.$month.'-1');  //当月的第一天
    $monthEnd = strtotime($year.'-'.$month.'-'.$t) + 86399;  //当月的最后一天

    return array($monthFirst,$monthEnd);
}

 

你可能感兴趣的:(php)