While searching a result of Loop Through Dates, here are some looping scenarios given below.
Let us see at first for ‘foreach‘ loop after that ‘while‘.
-
foreach Loop
123456789101112<?php$begin = new DateTime( '2018-09-30' );$end = new DateTime( '2018-10-05' );$end = $end->modify( '+1 day' );$interval = new DateInterval('P1D');$daterange = new DatePeriod($begin, $interval ,$end);foreach($daterange as $date){echo $date->format("Y-m-d") . "<br>";}?> -
foreach Loop with little bit difference
1234567891011<?php$begin = new DateTime('2018-09-30');$end = new DateTime('2018-10-05');$interval = DateInterval::createFromDateString('1 day');$period = new DatePeriod($begin, $interval, $end);foreach ($period as $dt) {echo $dt->format("l Y-m-d H:i:sn"). "<br>";}?> -
while Loop
1234567891011121314<?php// Set timezonedate_default_timezone_set('UTC');// Start date$date = '2018-09-30';// End date$end_date = '2018-10-05';while (strtotime($date) <= strtotime($end_date)) {echo $date ."<br>";$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));}?>
Hence Loop Through dates is achieved successfully.
Thank you for giving a platform for submitting user’s own post for displaying his views about Technical Knowledge to world’s other user.
If you like this post then please share it on Social Media Websites.
Thank you Webpreparations