Mar*_*rty 46
我曾经尝试过最令人沮丧的事情 - 但在这里!
<?php
/**
* Returns the amount of weeks into the month a date is
* @param $date a YYYY-MM-DD formatted date
* @param $rollover The day on which the week rolls over
*/
function getWeeks($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;
$timestamp = strtotime($date);
$first = strtotime($cut . "00");
$elapsed = ($timestamp - $first) / $daylen;
$weeks = 1;
for ($i = 1; $i <= $elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);
$day = strtolower(date("l", $daytimestamp));
if($day == strtolower($rollover)) $weeks ++;
}
return $weeks;
}
//
echo getWeeks("2011-06-11", "sunday"); //outputs 2, for the second week of the month
?>
Run Code Online (Sandbox Code Playgroud)
Iir*_*ayn 25
编辑:对"单行"这么多 - 需要变量以避免使用条件重新计算.当我在它时,抛出一个默认参数.
function weekOfMonth($when = null) {
if ($when === null) $when = time();
$week = date('W', $when); // note that ISO weeks start on Monday
$firstWeekOfMonth = date('W', strtotime(date('Y-m-01', $when)));
return 1 + ($week < $firstWeekOfMonth ? $week : $week - $firstWeekOfMonth);
}
Run Code Online (Sandbox Code Playgroud)
请注意,weekOfMonth(strtotime('Oct 31, 2011'));
将返回6
; 与OP的预期相反,一些罕见的月份有6个星期.2017年1月是另一个月,有6个ISO周 - 从周一开始,周日第一个月是在去年的一周.
对于starshine531,要返回0
该月的索引周,请更改return 1 +
为return 0 +
或return (int)
.
对于Justin Stayton来说,从星期日而不是星期一开始的几个星期我将使用,strftime('%U'
而不是date('W'
如下:
function weekOfMonth($when = null) {
if ($when === null) $when = time();
$week = strftime('%U', $when); // weeks start on Sunday
$firstWeekOfMonth = strftime('%U', strtotime(date('Y-m-01', $when)));
return 1 + ($week < $firstWeekOfMonth ? $week : $week - $firstWeekOfMonth);
}
Run Code Online (Sandbox Code Playgroud)
对于这个版本,2017-04-30现在是4月的第6周,而2017-01-31现在是第5周.
Phi*_*lar 13
public function getWeeks($timestamp)
{
$maxday = date("t",$timestamp);
$thismonth = getdate($timestamp);
$timeStamp = mktime(0,0,0,$thismonth['mon'],1,$thismonth['year']); //Create time stamp of the first day from the give date.
$startday = date('w',$timeStamp); //get first day of the given month
$day = $thismonth['mday'];
$weeks = 0;
$week_num = 0;
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0){
$weeks++;
}
if($day == ($i - $startday + 1)){
$week_num = $weeks;
}
}
return $week_num;
}
Run Code Online (Sandbox Code Playgroud)
大家好,我一整天都在努力想出这个代码,我终于想通了,所以我想我会和大家分享.
你需要做的只是在函数中加上一个时间戳,它会将周数返回给你.
谢谢
小智 6
这种方法存在问题.如果通过日期(假设2012/01/01是星期日)和"$ rollover"日是"星期日",那么这个函数将返回2.其中它实际上是第1周.我想我已经修复了以下功能.请添加评论以使其更好.
function getWeeks($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;
$timestamp = strtotime($date);
$first = strtotime($cut . "01");
$elapsed = (($timestamp - $first) / $daylen)+1;
$i = 1;
$weeks = 0;
for($i==1; $i<=$elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);
$day = strtolower(date("l", $daytimestamp));
if($day == strtolower($rollover))
{
$weeks++;
}
}
if($weeks==0)
{
$weeks++;
}
return $weeks;
}
Run Code Online (Sandbox Code Playgroud)
这是一个基于sberry数学解决方案但是使用PHP DateTime类的解决方案.
function week_of_month($date) {
$first_of_month = new DateObject($date->format('Y/m/1'));
$day_of_first = $first_of_month->format('N');
$day_of_month = $date->format('j');
return floor(($day_of_first + $day_of_month - 1) / 7) + 1;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
60395 次 |
最近记录: |