我想确定一个日期并计算出其星期数。
到目前为止,我有以下几点。它应该是42时返回24。
<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>
数字颠倒是巧合吗?还是我快到了?
我想确定一个日期并计算出其星期数。
到目前为止,我有以下几点。它应该是42时返回24。
<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>
数字颠倒是巧合吗?还是我快到了?
Answers:
今天,使用PHP的DateTime
对象更好:
<?php
$ddate = "2012-10-18";
$date = new DateTime($ddate);
$week = $date->format("W");
echo "Weeknummer: $week";
这是因为in中mktime()
,它是这样的:
mktime(hour, minute, second, month, day, year);
因此,您的订单是错误的。
<?php
$ddate = "2012-10-18";
$duedt = explode("-", $ddate);
$date = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>
Carbon::createFromFormat('Y-m-d H', '2012-10-18')->format('W');
mktime()
错误,而不是OP的顺序。当然,后者更容易更改。
使用PHP的日期函数
http://php.net/manual/en/function.date.php
date("W", $yourdate)
就像一个建议:
<?php echo date("W", strtotime("2012-10-18")); ?>
可能比所有这些都简单一些。
您可以做的其他事情:
<?php echo date("Weeknumber: W", strtotime("2012-10-18 01:00:00")); ?>
<?php echo date("Weeknumber: W", strtotime($MY_DATE)); ?>
我尝试解决这个问题已有多年了,我以为我找到了一个更短的解决方案,但是不得不再次回到长篇大论。此函数返回正确的ISO周符号:
/**
* calcweek("2018-12-31") => 1901
* This function calculates the production weeknumber according to the start on
* monday and with at least 4 days in the new year. Given that the $date has
* the following format Y-m-d then the outcome is and integer.
*
* @author M.S.B. Bachus
*
* @param date-notation PHP "Y-m-d" showing the data as yyyy-mm-dd
* @return integer
**/
function calcweek($date) {
// 1. Convert input to $year, $month, $day
$dateset = strtotime($date);
$year = date("Y", $dateset);
$month = date("m", $dateset);
$day = date("d", $dateset);
$referenceday = getdate(mktime(0,0,0, $month, $day, $year));
$jan1day = getdate(mktime(0,0,0,1,1,$referenceday[year]));
// 2. check if $year is a leapyear
if ( ($year%4==0 && $year%100!=0) || $year%400==0) {
$leapyear = true;
} else {
$leapyear = false;
}
// 3. check if $year-1 is a leapyear
if ( (($year-1)%4==0 && ($year-1)%100!=0) || ($year-1)%400==0 ) {
$leapyearprev = true;
} else {
$leapyearprev = false;
}
// 4. find the dayofyearnumber for y m d
$mnth = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
$dayofyearnumber = $day + $mnth[$month-1];
if ( $leapyear && $month > 2 ) { $dayofyearnumber++; }
// 5. find the jan1weekday for y (monday=1, sunday=7)
$yy = ($year-1)%100;
$c = ($year-1) - $yy;
$g = $yy + intval($yy/4);
$jan1weekday = 1+((((intval($c/100)%4)*5)+$g)%7);
// 6. find the weekday for y m d
$h = $dayofyearnumber + ($jan1weekday-1);
$weekday = 1+(($h-1)%7);
// 7. find if y m d falls in yearnumber y-1, weeknumber 52 or 53
$foundweeknum = false;
if ( $dayofyearnumber <= (8-$jan1weekday) && $jan1weekday > 4 ) {
$yearnumber = $year - 1;
if ( $jan1weekday = 5 || ( $jan1weekday = 6 && $leapyearprev )) {
$weeknumber = 53;
} else {
$weeknumber = 52;
}
$foundweeknum = true;
} else {
$yearnumber = $year;
}
// 8. find if y m d falls in yearnumber y+1, weeknumber 1
if ( $yearnumber == $year && !$foundweeknum) {
if ( $leapyear ) {
$i = 366;
} else {
$i = 365;
}
if ( ($i - $dayofyearnumber) < (4 - $weekday) ) {
$yearnumber = $year + 1;
$weeknumber = 1;
$foundweeknum = true;
}
}
// 9. find if y m d falls in yearnumber y, weeknumber 1 through 53
if ( $yearnumber == $year && !$foundweeknum ) {
$j = $dayofyearnumber + (7 - $weekday) + ($jan1weekday - 1);
$weeknumber = intval( $j/7 );
if ( $jan1weekday > 4 ) { $weeknumber--; }
}
// 10. output iso week number (YYWW)
return ($yearnumber-2000)*100+$weeknumber;
}
我发现我的短期解决方案错过了2018-12-31,因为它提供了1801而不是1901。所以我不得不输入这个正确的较长版本。
您的代码可以使用,但是您需要翻转第4个和第5个参数。
我会这样
$date_string = "2012-10-18";
$date_int = strtotime($date_string);
$date_date = date($date_int);
$week_number = date('W', $date_date);
echo "Weeknumber: {$week_number}.";
另外,一个星期不看该代码后,您的变量名将使您感到困惑,您应该考虑阅读http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer/
规则是,一年的第一周是包含该年的第一个星期四的那一周。
我个人使用Zend_Date进行这种计算,因此获得今天的星期很简单。如果您使用日期,它们还有许多其他有用的功能。
$now = Zend_Date::now();
$week = $now->get(Zend_Date::WEEK);
// 10
date('W', $date)
基础。不过,这并不是唯一的规则。
要获取日期2018-12-31的正确周数,请使用以下代码
$day_count = date('N',strtotime('2018-12-31'));
$week_count = date('W',strtotime('2018-12-31'));
if($week_count=='01' && date('m',strtotime('2018-12-31'))==12){
$yr_count = date('y',strtotime('2018-12-31')) + 1;
}else{
$yr_count = date('y',strtotime('2018-12-31'));
}
获取星期数 jalai日历中的您可以使用以下方法:
$weeknumber = date("W"); //number week in year
$dayweek = date("w"); //number day in week
if ($dayweek == "6")
{
$weeknumberint = (int)$weeknumber;
$date2int++;
$weeknumber = (string)$date2int;
}
echo $date2;
结果:
15
星期六的周数变化
上面给出的大多数示例在一年有53周(例如2020年)时都会产生问题。因此,每四年,您将经历一周的差异。此代码不:
$thisYear = "2020";
$thisDate = "2020-04-24"; //or any other custom date
$weeknr = date("W", strtotime($thisDate)); //when you want the weeknumber of a specific week, or just enter the weeknumber yourself
$tempDatum = new DateTime();
$tempDatum->setISODate($thisYear, $weeknr);
$tempDatum_start = $tempDatum->format('Y-m-d');
$tempDatum->setISODate($thisYear, $weeknr, 7);
$tempDatum_end = $tempDatum->format('Y-m-d');
echo $tempDatum_start //will output the date of monday
echo $tempDatum_end // will output the date of sunday
如何使用IntlGregorianCalendar
课程?
要求:开始使用之前,请IntlGregorianCalendar
确保已在服务器上安装libicu
或pecl/intl
。因此,在CLI上运行:
php -m
如果您intl
在[PHP Modules]
列表中看到,则可以使用IntlGregorianCalendar
。
DateTime与IntlGregorianCalendar:
IntlGregorianCalendar
当时还不错DateTime
。但是关于的好处IntlGregorianCalendar
是它可以给你星期的数字int
。
例:
$dateTime = new DateTime('21-09-2020 09:00:00');
echo $dateTime->format("W"); // string '39'
$intlCalendar = IntlCalendar::fromDateTime ('21-09-2020 09:00:00');
echo $intlCalendar->get(IntlCalendar::FIELD_WEEK_OF_YEAR); // integer 39
当您需要一年和一周时会变得更加困难。
尝试找出哪个星期是2017年1月1日。
(这是2016年的第52周,从2016年12月26日星期一开始-2017年1月1日星期日)。
经过更长的搜索,我发现
strftime('%G-%V',strtotime("2017-01-01"))
结果:2016-52
https://www.php.net/manual/de/function.strftime.php
给定年份的ISO-8601:1988周号,从每年的第一周开始,至少有4个工作日,从星期一开始一周中的。(01至53)
mysql中的等效项是DATE_FORMAT(date,'%x-%v')
https://www.w3schools.com/sql/func_mysql_date_format.asp
Week,其中星期一是一周的第一天(01到53)。
使用date()或DateTime找不到相应的解决方案。
至少并非没有诸如“ + 1day,last monday”之类的解决方案。
function last_monday($date)
{
if (!is_numeric($date))
$date = strtotime($date);
if (date('w', $date) == 1)
return $date;
else
return date('Y-m-d',strtotime('last monday',$date));
}
$date = '2021-01-04'; //Enter custom date
$year = date('Y',strtotime($date));
$date1 = new DateTime($date);
$ldate = last_monday($year."-01-01");
$date2 = new DateTime($ldate);
$diff = $date2->diff($date1)->format("%a");
$diff = $diff/7;
$week = intval($diff) + 1;
echo $week;
//Returns 2.
试试这个解决方案
date( 'W', strtotime( "2017-01-01 + 1 day" ) );