本文介绍了我在PHP中有两个日期,我该如何运行foreach循环来完成所有这些日子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用日期 2010-05-01 ,以$ code> 2010-05-10 结尾。如何在PHP中迭代所有这些日期?

I'm starting with a date 2010-05-01 and ending with 2010-05-10. How can I iterate through all of those dates in PHP?

推荐答案

需要PHP5.3:

$begin = new DateTime( '2010-05-01' );
$end = new DateTime( '2010-05-10' );

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ( $period as $dt )
  echo $dt->format( "l Y-m-d H:i:s\n" );

这将在 $ start $ end 。如果要包含第10个,将 $ end 设置为11。您可以根据自己的喜好调整格式。请参阅的PHP手册。

This will output all days in the defined period between $start and $end. If you want to include the 10th, set $end to 11th. You can adjust format to your liking. See the PHP Manual for DatePeriod.

这篇关于我在PHP中有两个日期,我该如何运行foreach循环来完成所有这些日子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:12