我有这个脚本,它返回2个日期之间的时间范围,但是增加1天!!
$date_1 = date("Y-m-d g:i:s", strtotime('2013-06-27 12:00:00'));
$date_2 = date("Y-m-d g:i:s", strtotime('2013-06-29 12:00:00'));
$results = array($date_1);
$i = $date_1;
while ($i <= $date_2) {
$i = date("Y-m-d g:i:s", strtotime("+1 day", strtotime($i)));// how do I take off here this "+1 day"
array_push($results, $i);
echo $i;
}
因此当我echo
$i
时,我会收到以下字符串
2013-06-28 2013-06-29 2013-06-30
我需要
2013-06-27 2013-06-28 2013-06-29
问题显然是“+ 1天”,但是如果我从我的功能中取消它就会出错。
如何解决这个问题?
答案 0 :(得分:0)
尝试更改此
strtotime("+1 day", strtotime($i))
到此:
strtotime("+0 day", strtotime($i))
答案 1 :(得分:0)
我试过了:
$date_1 = date("Y-m-d g:i:s", strtotime('2013-06-27 12:00:00'));
$date_2 = date("Y-m-d g:i:s", strtotime('2013-06-29 12:00:00'));
$results = array($date_1);
$i = $date_1;
while ($i <= $date_2) {
$i = date("Y-m-d g:i:s", strtotime($i));// how do i take off here this "+1 day"
array_push($results, $i);
echo $i . "\n";
$i = date("Y-m-d g:i:s",strtotime("+1 day", strtotime($i)));
}
然后给了我:
2013-06-27 12:00:00
2013-06-28 12:00:00
2013-06-29 12:00:00
答案 2 :(得分:0)
您需要增加变量$i
:
$date_1 = strtotime('2013-06-27 12:00:00');
$date_2 = strtotime('2013-06-29 12:00:00');
$results = array();
$i = $date_1;
while ($i <= $date_2) {
array_push($results, date("Y-m-d g:i:s", $i));
$i = strtotime('+1 day', $i);
}
print_r($results);
答案 3 :(得分:0)
只是为了让你知道:
date_default_timezone_set('UTC');
$date_from = '2013-06-27 12:00:00';
$date_interval = '+1 day';
$date_to = '2013-06-29 12:00:00';
$date_range = array_map(function ($date) {
return $date->format('Y-m-d g:i:s');
}, iterator_to_array(
new DatePeriod(new DateTime($date_from),
DateInterval::createFromDateString($date_interval),
(new DateTime($date_to))->modify('+1 day'))));
var_dump($date_range);
给我以下内容:
array(3) {
[0]=>
string(10) "2013-06-27 12:00:00"
[1]=>
string(10) "2013-06-28 12:00:00"
[2]=>
string(10) "2013-06-29 12:00:00"
}
PHP 5.3+兼容。