我正在尝试调用一个函数,它将返回从现在到过去的可变天数之间的所有日子。下面是一些伪代码与实代码的混合。你们能帮我把所有的日子都放回去吗?
function getTimeStamps($numDays){
$today = date("Y-m-d");
$startDate = $today - $numdays;
$movingDay = $startDate;
$results = array();
while($movingDay <= $today){
array_push($results,$movingDay);
$movingDay + 1 day;
}
return $results;
}
$dateList = getTimeStamps(8);此函数将返回
array(
'2013-12-10',
'2013-12-11',
'2013-12-12',
'2013-12-13',
'2013-12-14',
'2013-12-15',
'2013-12-16',
'2013-12-17'
);发布于 2013-12-17 22:07:15
这应该是你所需要的重物。您可以修改它以满足您的确切目的。
$start = new DateTime('2013-12-01');
$end = new DateTime('2013-12-17');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("Y-m-d") . PHP_EOL;
}看到它的行动
发布于 2013-12-17 22:18:27
function getTimeStamps($numDays){
$dates = array();
for ($i=$numDays-1; $i>=0; $i--){
$dates[] = date("Y-m-d", strtotime("now - $i days"));
}
return $dates;
}所以..。
print_r(getTimeStamps(8));打印出来:
Array
(
[0] => 2013-12-10
[1] => 2013-12-11
[2] => 2013-12-12
[3] => 2013-12-13
[4] => 2013-12-14
[5] => 2013-12-15
[6] => 2013-12-16
[7] => 2013-12-17
)发布于 2013-12-17 22:19:21
Johhn的回答很好;作为补充,这是一个使用更古老的时间戳的例子,包装在生成器中:
function getPastDates($daysAgo)
{
$current = strtotime(sprintf('-%d days', $daysAgo));
for ($i = 0; $i < $daysAgo; ++$i) {
yield $current;
$current = strtotime('+1 day', $current);
}
}
foreach (getPastDates(7) as $ts) {
echo date('Y-m-d', $ts), "\n";
}https://stackoverflow.com/questions/20645688
复制相似问题