如何查找当月的第2和第4个星期六。 我写了这些文字: -
echo "may 2nd sat ".date('d', strtotime('may 2013 second saturday'));
echo '<br/>may 4th sat '.date('d', strtotime('may 2013 fourth saturday'));
echo '<br/>june 2nd sat '.date('d', strtotime('june 2013 second saturday'));
echo '<br/>june 4th sat '.date('d', strtotime('june 2013 fourth saturday'));
它提供以下输出: -
may 2nd sat 11
may 4th sat 25
june 2nd sat 15
june 4th sat 29
它在may
个月给出了正确答案,但没有june 2013
,jun 1013
第二和第四个星期六分别为8和22。我怎么能解决这个问题。
答案 0 :(得分:7)
我不确定你为什么不会工作,但试试这个,它对我有用:
echo '<br/>june 2nd sat '.date('d', strtotime('second sat of june 2013'));
echo '<br/>june 4th sat '.date('d', strtotime('fourth sat of june 2013'));
中的"first sat of July 2008"
示例
答案 1 :(得分:4)
我不知道为什么它会导致这个错误,无论我找到解决方案如何得到正确答案
echo date('d',strtotime('+1 week sat may 2013')).'<BR>';
echo date('d',strtotime('+3 week sat may 2013')).'<BR>';
echo date('d',strtotime('+1 week sat june 2013')).'<BR>';
echo date('d',strtotime('+3 week sat june 2013')).'<BR>';
此解决方案正常运行并显示正确的结果。
<强>输出:强>
11
25
08
22
答案 2 :(得分:1)
这些天我首选的方法是扩展PHP的DateTime object: - ___
class MyDateTime extends DateTime
{
/**
* Returns a MyDateTime object set to 00:00 hours on the nth occurence
* of a given day of the month
*
* @param string $n nth day required, eg first, second etc
* @param string $day Name of day
* @param mixed $month Month number or name optional defaults to current month
* @param mixed $year optional defaults to current year
*
* @return MyDateTime set to last day of month
*/
public function nthDayOfMonth($n, $day, $month = null, $year = null)
{
$timestr = "$n $day";
if(!$month) $month = $this->format('M');
$timestr .= " of $month $year";
$this->setTimestamp(strtotime($timestr));
$this->setTime(0, 0, 0);
return $this;
}
}
$dateTime = new MyDateTime();
echo $dateTime->nthDayOfMonth('second', 'Sun', 'Jul', 2011)->format('Y-m-d');
输出: -
2011-07-10
答案 3 :(得分:1)
然后您必须运行版本低于5.2.7的PHP版本,作为manual states
在5.2.7之前的PHP 5中,请求在该工作日是该月的第一天的一个月中给定工作日的给定事件将错误地将一周添加到返回的时间戳。这已在5.2.7及更高版本中得到纠正。
因此,如果您可以更新您的PHP版本,那将是最佳解决方案。否则你可以查看类似的内容。
<?php
function showDay($month, $year, $day, $count)
{
$list = array(1=>'first',2=>'second',3=>'third',4=>'fourth',5=>'fifth');
$first = date('d', strtotime($month . ' ' . $year . ' ' . $list[1] .' '.$day));
$show= ($first>7) ? $count-1 : $count;
return date('d', strtotime($month . ' ' . $year . ' ' . $list[$show] .' '.$day));
}
echo '<br/>june 2nd sat '.showDay('june', 2013, 'saturday', 2);
?>
答案 4 :(得分:1)
我通常不依赖一根绳子。如何定制功能?
function getSaturdayDay($year, $month, $position) {
$firstDay = date('w', mktime(0, 0, 0, $month, 1, $year));
$diff = 6 - $firstDay;
return 1 + $diff + $position * 7;
}
并在您的上下文中使用
echo "may 2nd sat " . getSaturdayDay(2013, 5, 1);
echo '<br/>may 4th sat ' . getSaturdayDay(2013, 5, 3);
echo '<br/>june 2nd sat ' . getSaturdayDay(2013, 6, 1);
echo '<br/>june 4th sat ' . getSaturdayDay(2013, 6, 3);
答案 5 :(得分:0)
查找每月的第二个和第四个星期六:
private bool FindSecondSaturdayFourthSaturday()
{
bool IsHoliday = false;
DateTime TodaysDate = DateTime.Today;
DateTime SecondSaturday;
DateTime FourthSaturday;
//SecondSaturday will be after 8
SecondSaturday = Enumerable.Range(8, 7)
.Select(item => new DateTime(TodaysDate.Year, TodaysDate.Month, item))
.Where(date => date.DayOfWeek == DayOfWeek.Saturday)
.Single();
//Lsat Saturday will be after 22
FourthSaturday = Enumerable.Range(22, 7)
.Select(item => new DateTime(TodaysDate.Year, TodaysDate.Month, item))
.Where(date => date.DayOfWeek == DayOfWeek.Saturday)
.Single();
return IsHoliday;
}
答案 6 :(得分:0)
每年获取第二个和第三个星期六
public function getSndFthSaturday()
{
$now = strtotime("01-01-2019");
$end_date = strtotime("31-12-2019");
$this->calculate($now, $end_date);
}
public function calculate($now, $end_date)
{
while (date("Y-m-d", $now) != date("Y-m-d", $end_date))
{
$day_index = date("w", $now);
$day_indexD = floor((date("d", $now) - 1)/ 7);
if ($day_index == 6 && ($day_indexD == 1 || $day_indexD == 3)) {
$now1 = date("Y-m-d", $now);
print_r($now1);
echo "</br>";
}
$now = strtotime(date("Y-m-d", $now) . "+1 day");
}
}