固定时间段之间的所有日期组合(PHP/循环)

发布于 2024-12-09 02:46:23 字数 328 浏览 0 评论 0原文

这听起来像是一个愚蠢的问题,但我正在努力思考如何最好地解决它。

我有 2 个日期,比如说 2012 年 3 月 15 日到 2012 年 3 月 19 日。我需要提取从所有 4 天到 4 天的所有组合,输出如下(为简单起见,更改格式):

15, 16, 17, 18
15,16, 17
16, 17, 18
15, 16
16, 17
17, 18
15
16
17
18

我正在寻找最佳的循环方法,日期范围的长度会有所不同,格式不会相关,但它需要是 php 日期,我将从中提取所需的确切格式。我将使用每个日期集在每次迭代时执行代码,因此必须是日期/循环。

This sounds like a silly question but I am struggling to think of how best to tackle it.

I have 2 dates, lets say 15-03-2012 to 19-03-2012. I need to extract all combinations working down from all 4 days together to 4 individual days, output like this (formatting change for simplicity):

15, 16, 17, 18
15,16, 17
16, 17, 18
15, 16
16, 17
17, 18
15
16
17
18

I am looking for the best looping method, the date range will vary in length, the format isn't relevant but it needs to be php dates from which I will extract the exact format required. I will be using each date set to execute code at each iteration so has to be dates/loops.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛刀软妹 2024-12-16 02:46:23

创建两个您拥有的值的 Date 对象,编写一个 while 循环,运行直到开始变量(您的开始日期)等于结束变量(您的结束日期),您应该完成。

Create two Date objects of the values you have, write a while loop that runs until the start variable (your start date) is equal to the end variable (your end date) and you should be done.

只怪假的太真实 2024-12-16 02:46:23

总体思路:

$day = 24 * 60 * 60;

for($startDate = $dateA; $startDate <= $dateB; $startDate += day)
{
  for($endDate = $startDate; $endDate <= $dateB; $endDate += day)
  {
    for($dateIndex = startDate; $dateIndex <= $endDate; $dateIndex += day)
    {
      $output = getDate($dateIndex); 
      echo($output['month'] . $output['day'] . ',');
    }
    echo '</br>';
  }
}

The general idea:

$day = 24 * 60 * 60;

for($startDate = $dateA; $startDate <= $dateB; $startDate += day)
{
  for($endDate = $startDate; $endDate <= $dateB; $endDate += day)
  {
    for($dateIndex = startDate; $dateIndex <= $endDate; $dateIndex += day)
    {
      $output = getDate($dateIndex); 
      echo($output['month'] . $output['day'] . ',');
    }
    echo '</br>';
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文