我在PHP有2个date,我怎么能运行一个foreach循环来经历所有这些日子?
我以2010-05-01
开始, 2010-05-01
结束。 我怎样才能遍历PHP中的所有这些date?
需要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 Ymd H:i:s\n" );
这将在$start
和$end
之间的定义期间内输出所有日子。 如果你想包括第十,将$end
设置为11。 您可以根据自己的喜好调整格式。 请参阅DatePeriod的PHP手册。
这也包括最后的date
$begin = new DateTime( "2015-07-03" ); $end = new DateTime( "2015-07-09" ); for($i = $begin; $i <= $end; $i->modify('+1 day')){ echo $i->format("Ymd"); }
如果你不需要最后的date,只需从条件中删除=
。
转换为unix时间戳使得datemath在php中更简单:
$startTime = strtotime( '2010-05-01 12:00' ); $endTime = strtotime( '2010-05-10 12:00' ); // Loop between timestamps, 24 hours at a time for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) { $thisDate = date( 'Ym-d', $i ); // 2010-05-01, 2010-05-02, etc }
当使用带有DST的时区的PHP时,请确保添加非23:00,00:00或1:00的时间以防止日子跳过或重复。
$startTime = strtotime('2010-05-01'); $endTime = strtotime('2010-05-10'); // Loop between timestamps, 1 day at a time $i = 1; do { $newTime = strtotime('+'.$i++.' days',$startTime); echo $newTime; } while ($newTime < $endTime);
要么
$startTime = strtotime('2010-05-01'); $endTime = strtotime('2010-05-10'); // Loop between timestamps, 1 day at a time do { $startTime = strtotime('+1 day',$startTime); echo $startTime; } while ($startTime < $endTime);
使用此function: –
function dateRange($first, $last, $step = '+1 day', $format = 'Ymd' ) { $dates = array(); $current = strtotime($first); $last = strtotime($last); while( $current <= $last ) { $dates[] = date($format, $current); $current = strtotime($step, $current); } return $dates; }
用法/函数调用: –
增加一天: –
dateRange($start, $end); //increment is set to 1 day.
按月份增加: –
dateRange($start, $end, "+1 month");//increase by one month
如果您想设置date格式,请使用第三个参数: –
dateRange($start, $end, "+1 month", "Ymd H:i:s");//increase by one month and format is mysql datetime
从包含范围的php.net示例复制:
$begin = new DateTime( '2012-08-01' ); $end = new DateTime( '2012-08-31' ); $end = $end->modify( '+1 day' ); $interval = new DateInterval('P1D'); $daterange = new DatePeriod($begin, $interval ,$end); foreach($daterange as $date){ echo $date->format("Ymd") . "<br>"; }
这是另一个简单的 –
/** * Date range * * @param $first * @param $last * @param string $step * @param string $format * @return array */ function dateRange( $first, $last, $step = '+1 day', $format = 'Ymd' ) { $dates = []; $current = strtotime( $first ); $last = strtotime( $last ); while( $current <= $last ) { $dates[] = date( $format, $current ); $current = strtotime( $step, $current ); } return $dates; }
例:
print_r( dateRange( '2010-07-26', '2010-08-05') ); Array ( [0] => 2010-07-26 [1] => 2010-07-27 [2] => 2010-07-28 [3] => 2010-07-29 [4] => 2010-07-30 [5] => 2010-07-31 [6] => 2010-08-01 [7] => 2010-08-02 [8] => 2010-08-03 [9] => 2010-08-04 [10] => 2010-08-05 )
这是一个方法:
$date = new Carbon(); $dtStart = $date->startOfMonth(); $dtEnd = $dtStart->copy()->endOfMonth(); $weekendsInMoth = []; while ($dtStart->diffInDays($dtEnd)) { if($dtStart->isWeekend()) { $weekendsInMoth[] = $dtStart->copy(); } $dtStart->addDay(); }
$ weekendsInMoth的结果是周末的日子!