PHP获取每周星期从具体的开始日期 [英] PHP Get recurring week from specific start date
问题描述
我期待到试图建立和阵列,这将是这个样子:
I'm looking into trying to set up and array that will look something like this:
$dates = array(
[0] => "07/11/2013",
[1] => "14/11/2013",
[2] => "21/11/2013",
[3] => "28/11/2013",
[4] => "05/12/2013",
[5] => "12/12/2013");
我愿意用这个,但我想明年再此再次发生我preFER有PHP做到这一点,它进入我的一个数组。我知道如何限制它,我希望有一个具体金额,但我不知道怎么一个星期添加到当前日期或特定的日期,如果我想启动 2013年8月11日
例如
我已经快看,似乎我无法找到任何这一点。
I've had a quick look and I can't seem to find anything that does this.
我只需要一个脚本来一个星期添加到当前为止,此刻,这是逢星期四,然后添加到阵列中。
I just need a script to add a week to the current date, at the moment this is every Thursday, and then add that to the array.
我唯一的问题是我不知道如何指定一个日期,然后每次添加一个星期。我假设为
循环将是最好的位置。
My only problem is I'm not sure how to specify a date, and then add a week every time. I assume a for
loop would be best here.
推荐答案
使用 的DateTime
类。 DateInterval和DatePeriod类在PHP 5.3.0相继出台,所以下面的解决方案适用于仅PHP> = 5.3.0:
Use DateTime
class. DateInterval and DatePeriod classes were introduced in PHP 5.3.0, so the below solution works for only PHP >= 5.3.0:
$start = new DateTime('2013-11-07');
$end = new DateTime('2013-12-31');
$interval = new DateInterval('P1W'); // one week
$p = new DatePeriod($start, $interval, $end);
foreach ($p as $w) {
$weeks[] = $w->format('d-m-Y');
}
由于Glavic注意到,<一个href=\"http://stackoverflow.com/questions/19840912/php-get-recurring-week-from-specific-start-date/19841137#comment29529922_19841137\">comments下面,这也可以在PHP的previous版本使用完成修改()
方式:
As Glavic notes in the comments below, this can also be done in previous versions of PHP using the modify()
method:
$start = new DateTime('2013-11-07');
$end = new DateTime('2013-12-31');
$weeks = array();
while ($start < $end) {
$weeks[] = $start->format('d-m-Y');
$start->modify('+1 week');
}
这篇关于PHP获取每周星期从具体的开始日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!