在 PHP 中获取两个指定日期之间的日期
给定文本框中的两个日期(例如,“20-4-2010”和“ 22-4-2010”),目标是提取它们之间的日期并将其格式设置为“20,21,22”。
PHP 解决方案:
要实现此目的,请使用以下 PHP 代码:
<code class="php">$start = strtotime('20-04-2010 10:00'); $end = strtotime('22-04-2010 10:00'); for ($current = $start; $current <p><strong>说明:</strong></p> <ul> <li>strtotime() 将日期转换为具有特定时间分量的 Unix 时间戳以防止时区问题。</li> <li>for 循环迭代时间戳,以一天(86400 秒)递增。</li> <li>date() 将每个时间戳格式化为所需格式的日期。</li> </ul> <p><strong>其他选项:</strong></p> <ul><li>指定天数而不是使用结束日期:</li></ul> <pre class="brush:php;toolbar:false"><code class="php">for ($i = 0; $i <ul> <li>使用 PHP 5.3 的 DatePeriod 类:</li> </ul> <pre class="brush:php;toolbar:false"><code class="php">$period = new DatePeriod( new DateTime('20-04-2010'), DateInterval::createFromDateString('+1 day'), new DateTime('23-04-2010') ); foreach ($period as $dt) { echo $dt->format('d-m-Y'); }</code>
以上是如何在 PHP 中提取两个日期之间的日期范围?的详细内容。更多信息请关注PHP中文网其他相关文章!