【问题标题】:PHP | How to find EVERY OTHER SUNDAY from SECOND SUNDAY of THIS YEAR to second sunday of NEXT YEARPHP |如何找到从今年的第二个星期日到明年的第二个星期日的每个星期天
【发布时间】:2015-01-28 09:47:15
【问题描述】:

背景:我的客户是一家当地的电影院,从每年的第二个星期日开始,每隔一个星期天都会举办一次周日日场特别节目。所以今年的日期是 1/11, 1/18, 2/8, 2/22, .... [唯一的例外是他们的电影节后的星期天,它在 10 月的第三周举行,但自动化这个唯一的例外是一个“会很好”的项目,不是必需的。]

我的技能水平:初学者(我需要你的帮助!)我相信我需要结合使用 mktime() 和 date() 但我不知道如何将它们组合在一起。

我的尝试:我怀疑答案是我在这三个帖子中看到的组合:

(1)a do-while loop to get a specific day of the week from a date range

(2)there may be a shortcut for referencing the second sunday in the ACCEPTED ANSWER here, but I'm not sure this helps

(3)MOST RELEVANT(?): Get the First Sunday of Every Month

END 结果:我想显示下周日 Matinee 的 [Month] 和 [Day](所以我想在当前日期之后查找并显示数组中的第一项)。文本将如下所示:“下一个:[月] [日]”

有意义吗?如果我忘记了什么,请告诉我。

如果问的不是太多,请解释您的代码,以便我(和其他人?)可以从中学习;但我会非常感谢“仅仅”一个直接的解决方案。

非常感谢。 黛布拉

更新/进度:此代码将为我提供周日数组:

$startDate = strtotime("second Sunday of ".date('Y').""); for ($i=0; $i < 27; $i++){ $sundays = date('F j', ($startDate + (($i*14) * 24 * 3600))) . '<br>'; print $sundays; }

接下来要弄清楚:编写一个语句以在星期日数组中查找当前日期之后的第一个日期。

【问题讨论】:

  • 发布您自己尝试过的任何代码。
  • @Tom,我只有这些。第 1 步:创建星期日数组(从一年中的第 2 个星期日开始,以这种格式显示:1 月 11 日)。这让我得到了我的开始日期: $startDate = date("F jS", strtotime("second Sunday of ".date('Y').""));现在我想制作星期天的数组: $sundaysArray = array();我不知道如何编写循环函数以每隔一个星期日从 $startDate 获取我,并在数组中有 26 个项目时停止(26 将让我每隔一个星期日获取一年)。 (如何换行??)
  • 看起来这种语言会让我在 2 周的星期天工作:$startDate += (14 * 24 * 3600);但是,再一次,我不知道如何将它们放在一起。

标签: php date mktime


【解决方案1】:

这是一个非常手动的程序解决方案,但它应该可以工作。

<?php
$SECS_PER_DAY = 86400;

# Find the first Sunday on or after a given timestamp
function firstSundayOnOrAfter($time) {
  global $SECS_PER_DAY;

  # What day of the week is the given date?
  $wday = date('w', $time);

  if ($wday == 0) {
    # it's already a Sunday
    return $time;
  } 

  return $time + (7 - $wday) * $SECS_PER_DAY;
}

# return an array of timestamps representing 
# (noon on) the special matinee Sundays for the given year
function specialMatineeSundays($year) {

  global $SECS_PER_DAY;

  # When's the film festival?
  $oct1 = mktime(12,0,0,10,1,$year);
  $festivalStart = firstSundayOnOrAfter($oct1);
  $festivalSkip  = $festivalStart + 7 * $SECS_PER_DAY;

  # find the first Sunday of the year
  $jan1 = mktime(12,0,0,1,1,$year);
  $sunday = firstSundayOnOrAfter($jan1);

  # Add a week to start with the second Sunday
  $sunday += 7 * $SECS_PER_DAY;

  # Build up our result list
  $result = [];

  # As long as the Sunday under examination is still the same year, 
  # add it to the list (unless it's the post-festival skip date)
  # and then add two weeks 
  while (date('Y',$sunday) == $year) {
    if ($sunday != $festivalSkip) {
      $result[] = $sunday;
    }
    $sunday += 14 * $SECS_PER_DAY;
  }

  return $result;
}

【讨论】:

  • 谢谢@mark,我从中学到了很多。这会比我想出的“for-loop”更好吗(参见我第一篇文章中的“更新/进度”)?关于代码第一部分的一个问题:而不是给定时间戳之后的第一个星期日,我需要数组 ($result) 中遵循给定时间戳的星期日。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多