简体   繁体   English

如何从日期中获取周数

[英]How to get week number from a date

I want to get the week number from a specific date Like so if project start date is 01-12-2020, and today is 14-12-2020我想从特定日期获取周数就像这样,如果项目开始日期是 01-12-2020,而今天是 14-12-2020

the current week number is 2当前周数为2

thanks谢谢

use this First get 1day of a date and get its week no cuz php provide year week no.使用此首先获取日期的 1 天并获取其周数,因为 php 提供年份周数。 now get current date week no and subtract it you will get the current week no:现在获取当前日期周号并减去它,您将获得当前周号:

$month= (date('Y-m-01'));
$yearweek = date('W');
$monthweek = date('W',strtotime($month));
echo  $yearweek - $monthweek;

You calculate how many weeks there are between two dates.您计算两个日期之间的周数。 To do this, calculate the difference in days and divide that by 7. The week number (Project week) is then the whole part plus 1.为此,请计算天数差并将其除以 7。周数(项目周)是整个部分加 1。

$start = date_create('01-12-2020');
$curDate = date_create('14-12-2020');
$weekNumber = (int)($start->diff($curDate)->days/7)+1;

You should try with:你应该尝试:

$now = Carbon::now();
echo $now->weekOfYear;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM