[英]List Weeks of Year in a Month in PHP
The scenario I have here is that, I need to select a Year and Month .我在这里的情况是,我需要选择Year和Month 。 And I need to show the Weeks in that currently selected Month .
我需要显示周,目前选定的一个月。 Let me explain it here ...
让我在这里解释一下......
2017 -> Oct -> Week N
( this should be the Week of year . Ex this week is #40 for 2017
.) So I need to list number of weeks that lies in Oct for this year. 2017 -> Oct -> Week N
(这应该是一年中的一周。例如,本周是#40 for 2017
周。)所以我需要列出今年 10 月的周数。
Thank you.谢谢。
Considered Michel's solution How to get year-sensitive calendar week ?考虑了米歇尔的解决方案如何获得对年份敏感的日历周? to avoid excess week from previous december to current january.
避免从去年 12 月到今年 1 月多出一周。
function getWeekNo( $date ) {
$t = strtotime($date) ;
$w=(int)date('W', $t);
$m=(int)date('n', $t);
$w=($w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w));
return $w ;
}
$month = '10-2017' ;
$firstDay = '01-' . $month ;
$lastDay = Date('t-m-Y', strtotime($firstDay)) ;
$firstWeek = getWeekNo($firstDay) ;
$lastWeek = getWeekNo($lastDay) ;
for( $i = $firstWeek; $i <= $lastWeek; $i++ ) {
//echo "Week $i <br/>";
echo "Week " . ($i + 1) . "<br/>"; //if you want to start january 1st as Week 1
}
Based on Desai's answer, I tried this on Carbon PHP ( on Laravel 5.5 ).根据 Desai 的回答,我在 Carbon PHP(在 Laravel 5.5 上)上进行了尝试。 I'm posting my answer in case anyone is looking for this.
如果有人正在寻找这个,我正在发布我的答案。
$month = 10;
$year = 2017;
$date = \Carbon\Carbon::create($year,$month);
$startOfMonth = $date->startOfMonth();
$endOfMonth = $date->copy()->endOfMonth();
$startWeek = $startOfMonth->format('W');
$endWeek = $endOfMonth->copy()->format('W');
for( $i = $startWeek; $i <= $endWeek; $i++ )
{
echo "Week #" . $i;
}
Thank you all for your answers.谢谢大家的回答。 Have a great day.
有一个美好的一天。
Try this code: Get first day and last day of month, count week numbers and loop over it试试这个代码:获取一个月的第一天和最后一天,计算周数并循环遍历它
$year = "2017";
$month = "10";
$day = "01";
$first_date = $year."-".$month."-".$day;
$last_date = date("Y-m-t",strtotime($first_date));
$first_week = date("W",strtotime($first_date));
$last_week = date("W",strtotime($last_date));
for($i=$first_week;$i<=$last_week;$i++)
{
echo "Week #".$i.", ";
}
Note that October has total 6 weeks.请注意,十月总共有 6 周。 1st date on Sunday last on Tuesday
星期二的最后一个星期日的第一次约会
According to carbon documentation at CarbonDocs根据CarbonDocs 的碳文档
$dt = Carbon::parse('2012-9-5 23:26:11.123789');
var_dump($dt->weekOfYear);
Sorry, I can't reply yet to the first comment, but the above solution is not working correctly if the last day of month have in first week of new year (test case: 2019-12-01).抱歉,我还不能回复第一条评论,但是如果一个月的最后一天在新年的第一周(测试用例:2019-12-01),则上述解决方案无法正常工作。
I made a quick fix (based on original code):我做了一个快速修复(基于原始代码):
// Input date format: YYYY-mm-dd
function getMonthWeekNumbers( $first_date )
{
$last_date = date( "Y-m-t",strtotime( $first_date ) );
$first_week = date( "W",strtotime( $first_date ) );
$last_week = date( "W",strtotime( $last_date ) );
// If last day of month have on first week on next year, subtract a week from the date
if( $last_week == '01' )
{
$last_week = date( "W",( strtotime( $last_date )-604800 ) );
}
for( $i=$first_week; $i <= $last_week; $i++ )
{
echo "Week #".$i.", ";
}
}
It working fine with any input dates.它适用于任何输入日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.