【问题标题】:geting specific dates in the future with php使用 php 获取未来的特定日期
【发布时间】:2015-01-08 04:43:02
【问题描述】:

所以我的问题很简单,但解决方案一直在逃避我。我正在尝试以设定的时间间隔获取未来日期的列表,该时间间隔尊重月份中的第 nr 周和每周中的第 nr 天。例如,我需要列出相隔 3 个月的第 2 个星期一的日期。

我有一个用户指定 2 个日期和时间间隔的表单。

所以我所拥有的是这样的:

$start_date = "2015-01-07";
$end_date = "2016-01-07";
$period = "+1 month";

然后,使用此链接 stackoverflow 中的此功能,我得到我的 start_date 是什么 weeknr:(即:“第一个星期一”)

function literalDate($timestamp) {
    $timestamp = is_numeric($timestamp) ? $timestamp : strtotime($timestamp);
    $weekday = date('l', $timestamp);
    $month = date('M', $timestamp);

$ord = 1;

while(date('M', ($timestamp = strtotime('-1 week', $timestamp))) == $month) {
    $ord++;
}

$lit = array(null, 'first', 'second', 'third', 'fourth', 'fifth');

return $lit[$ord].' '.$weekday;
}
$day_number = literalDate($start_date);
$list=array();

那么。我正在尝试这样做

while(strtotime($start_date) <= strtotime($end_date))
{
  $list[]=$start_date;
  $start_date=date('Y-m-d', strtotime("$start_date $period"));
  $month = date('F', strtotime($start_date));
  $start_date = date('Y-m-d', strtotime($day_number.' of '.$month)); //this always returns 1970-01-01
}

print_r($list);

如评论中所问,我的预期输出类似于

array(
[0] => 2015-01-07
[1] => 2015-02-04
[2] => 2015-03-04
[3] => 2015-04-01
[4] => 2015-05-06
[5] => 2015-06-03
[6] => 2015-07-01
[7] => 2015-08-05
[8] => 2015-09-02
[9] => 2015-10-07
[10] => 2015-11-04
[11] => 2015-12-02
)

【问题讨论】:

  • 显示您提供的示例的预期输出。
  • 您在字符串和整数之间错开约会,就像醉酒在路上蹒跚而行。一旦你对一个字符串进行了 strtotimed,LEAVE 它作为一个整数,因为你总是会在下一行代码中再次将它转换回一个 int。
  • 由于某种原因,我的大脑无法理解 strtotime 函数(是的,我读了 phpman 大约 1000 次)。所以因此我不知道如何将句点添加到 $start_date 而它是字符串。这就是滥用转换的全部原因

标签: php date


【解决方案1】:

据我了解,DateInterval 应该可以满足您的需求:

$start    = new DateTime('2015-01-07');
$start->modify('first day of this month');
$end      = new DateTime('2016-01-07');
$end->modify('first day of this month');
$interval = DateInterval::createFromDateString('first wednesday');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    $d = $dt->format("d");
    if($d <= 7){
       echo $dt->format("Y-m-d") . "\n";
    }
}

将显示:

2015-01-07
2015-02-04
2015-03-04
2015-04-01
2015-05-06
2015-06-03
2015-07-01
2015-08-05
2015-09-02
2015-10-07
2015-11-04
2015-12-02
2016-01-06

【讨论】:

  • 太棒了,我可以将“X 个月”添加到对象并移动特定日期,如“第二个星期日”,然后打印出来。
  • 是的,请注意 if 语句,我有它小于 7 只显示第一个星期一,所以你必须改变它
  • 谢谢伙计,我已经找到了如何增加指定的月数并很好地调整它,这正是我所需要的!我欠你很多啤酒,这让我整天都在烦恼! (ps:必须为此更新php,但值得)
  • 哈哈哈,没问题。我总是将这个类(DateInterval)用于任何日期范围,它很棒
  • 如果你没有写下你的答案,我今晚回家时会写成这样。
【解决方案2】:

strtotime() 在今天的参考中起作用。

strtotime("+x days");

strtotime

如果您需要与另一个日期有关的东西...

date("Y-m-d", strtotime($yourdate) + 86400 * $days);

【讨论】:

  • 那不起作用,因为我不想在日期上添加 x 时间量。我希望日期是特定间隔(1 个月、3 个月等)的第一个、第二个、第三个、最后一个等星期一,减去 4 天,最后到下一个星期一,1 年内 start_date 和 end_date 之间相差 2、3 周。我需要更准确的东西
  • 我给你工具来做这件事,而不是为你做。很抱歉,您觉得社区应该为您做这一切。
  • 不,伙计,我不想让你为我做这件事,因为你可以从我的代码中推断出我已经知道 strtotime。正如我的评论中所解释的,您的回答对我没有一点帮助,因为我需要一个月中一周中的特定日子。你的回答没有提供。只是让你知道,如果那是困扰你的原因,我并没有否决你的答案。
猜你喜欢
  • 2023-03-27
  • 2023-01-29
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多