【问题标题】:How to loop from today to one year later (one year date range) in PHP [duplicate]如何在PHP中从今天循环到一年后(一年日期范围)[重复]
【发布时间】:2011-06-10 09:54:48
【问题描述】:

可能的重复:
PHP Day iterator
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?

我想创建一个循环,为从今天到一年后的每一天创建记录。

没有日期输入。 从表格提交之日起至一年后的那一天。

我该怎么做?

例如,当我今天提交表单时,我需要从 17/01/2011 到 17/01/2012 的循环

【问题讨论】:

标签: php loops


【解决方案1】:
<?php 
echo date('Y-m-d', strtotime('+1 year'));

【讨论】:

  • 这只返回下一年的日期。 OP 正在查找该期间的所有日期
【解决方案2】:

这应该可以工作

$next_year = strtotime('+1 year');
$current_time = time();

while($current_time < $next_year){
    $current_time = strtotime('+1 day', $current_time);
    print date('d-m-Y', $current_time);
}

然后,您可以使用$current_time 变量和date() 以您希望的任何方式格式化时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多