【发布时间】: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 而它是字符串。这就是滥用转换的全部原因