Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php获取未来的特定日期_Php_Date - Fatal编程技术网

使用php获取未来的特定日期

使用php获取未来的特定日期,php,date,Php,Date,所以我的问题很简单,但解决方案一直在回避我。我试图以设定的时间间隔获得未来日期的列表,该时间间隔考虑月内的周数和周内的日数。例如,我需要列出相隔3个月的第二个星期一的日期 我有一个表单,其中用户指定2个日期和间隔 所以我得到的是: $start_date = "2015-01-07"; $end_date = "2016-01-07"; $period = "+1 month"; 然后,通过这个链接使用这个函数,我得到了我的开始日期(即:“第一个星期一”) 那么。我正试着这么做 while(s

所以我的问题很简单,但解决方案一直在回避我。我试图以设定的时间间隔获得未来日期的列表,该时间间隔考虑月内的周数和周内的日数。例如,我需要列出相隔3个月的第二个星期一的日期

我有一个表单,其中用户指定2个日期和间隔

所以我得到的是:

$start_date = "2015-01-07";
$end_date = "2016-01-07";
$period = "+1 month";
然后,通过这个链接使用这个函数,我得到了我的开始日期(即:“第一个星期一”)

那么。我正试着这么做

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);
strotime(),它在今天的引用中起作用

strtotime("+x days");

如果你需要与另一个约会有关的东西

日期(“Y-m-d”,标准时间($yourdate)+86400*$days)


据我所知,
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";
    }
}

显示您提供的示例的预期输出。您在字符串和整数之间错开日期,就像醉汉在路上错开一样。一旦你对一个字符串进行了strotime,就把它作为一个整数,因为你总是要在下一行代码中将它转换回int。出于某种原因,我的大脑并没有被设计为理解strotime函数(是的,我读了大约1000次phpman)。因此,我不知道如何在没有字符串的情况下将句点添加到$start_date。这就是滥用转换不起作用的全部原因,因为我不想在日期上增加x个时间。我希望日期是特定时间间隔(1个月、3个月等)的第一个、第二个、第三个、最后一个等星期一。我成功地做了类似的事情,但如果我有一个月的时间段,加上1个月,减去4天,最后到下一个星期一,那么在1年内开始日期和结束日期之间相差2、3周。我需要更精确的东西,我给你工具来做,而不是为你做。很抱歉,你觉得社区应该为你做这一切。不,伙计,我不想让你为我做这件事,因为你可以从我的代码中推断出我已经知道strotime了。正如我在评论中所解释的,你的回答对我一点帮助都没有,因为我需要一周中的某一天。你的回答没有提供这一点。你知道,我没有否决你的答案,如果这是困扰你的问题。那太棒了,我可以在这个对象上加上“X个月”,把它移动到一个特定的日期,比如“第二个星期天”,然后打印出来。是的,注意如果声明,我只有不到7个星期一才显示第一个星期一,所以你必须改变这个想法,谢谢,伙计,我已经找到了如何增加指定的月数的方法,并且很好地将它倾斜起来,这正是我所需要的!我欠你很多啤酒,这让我一整天都很烦!(ps:不得不为此更新php,但值得)哈哈,没问题。我总是用这个类(DateInterval)来表示任何日期范围。如果你没有写下你的答案,我今晚到家的时候就会这样写了。
strtotime("+x days");
$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