How to get the week of the month for a date in php

藏色散人
Release: 2023-03-11 12:50:01
Original
3976 people have browsed it

php method to get the week of the month for a date: first create a PHP sample file; then pass "date('d', $time)-(8 - $wk_day);echo $day<=0 ? 1:ceil($day / 7) 1;"Get the day of the month and the week of the month.

How to get the week of the month for a date in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php get the week of the month when the date is?

The PHP acquisition date belongs to the first week of the month. The simplest acquisition day is the first week of the month.

<?php
$time = strtotime(&#39;2019-01-01&#39;);
$wk_day = date(&#39;w&#39;, strtotime(date(&#39;Y-m-1 00:00:00&#39;, $time))) ? : 7; //今天周几
$day = date(&#39;d&#39;, $time) - (8 - $wk_day); //今天几号
echo $day <= 0 ? 1 : ceil($day / 7) + 1; //计算是第几个星期
 
print_r(week_of_month(strtotime(&#39;2019-01-1&#39;)));
 
function week_of_month($time, $offset = 1){
        $week = get_week_block($time);
        $wk_day = date(&#39;w&#39;, strtotime(date(&#39;Y-m-1 00:00:00&#39;, $time))) ? : 7; //今天周几
        $day = date(&#39;d&#39;, $time) - (8 - $wk_day); //今天几号
        $weekof = $day <= 0 ? 1 : ceil($day / 7) + 1; //计算是第几个星期
        if($offset){
            $week[0] = date(&#39;Y-m-d&#39;, max(strtotime(date(&#39;Y-m-1 00:00:00&#39;, $time)), $week[0]));
            $week[1] = date(&#39;Y-m-d&#39;, min(strtotime(date(&#39;Y-m-&#39;.date(&#39;t&#39;, $time).&#39; 23:59:59&#39;, $time)), $week[1]));
        }else{
            $week[0] = date(&#39;Y-m-d&#39;, $week[0]);
            $week[1] = date(&#39;Y-m-d&#39;, $week[1]);
        }
        return [&#39;week&#39; => $week, &#39;weekof&#39; => $weekof];
}
 
function get_week_block($sdefaultDate = false, $first = 1){
//当前日期    
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期  
if(!$sdefaultDate)$sdefaultDate = time();
if(is_numeric($sdefaultDate))$sdefaultDate = date(&#39;Y-m-d&#39;, $sdefaultDate);
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6  
$w=date(&#39;w&#39;, strtotime($sdefaultDate));  
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天  
$week_start=date(&#39;Y-m-d&#39;,strtotime("$sdefaultDate -".($w ? $w - $first : 6).&#39; days&#39;));  
//本周结束日期  
$week_end=date(&#39;Y-m-d&#39;,strtotime("$week_start +6 days"));
return array(strtotime($week_start.&#39; 00:00:00&#39;), strtotime($week_end.&#39; 23:59:59&#39;));
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get the week of the month for a date in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template