PHP: Return All Dates Between Two Dates in an Array

PHP: Return all dates between two dates in an array

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}

Calculate all dates between 2 dates in php?

Without loop using range() & array_map() :

EDIT: a little mistake, you have to jump 86400, because 1 day = 86400 seconds, so the code should be fine now :)


    $st_date = '2012-07-20';
$ed_date = '2012-07-27';
$dates = range(strtotime($st_date), strtotime($ed_date),86400);
$range_of_dates = array_map("toDate", $dates);
print_r($range_of_dates);
function toDate($x){return date('Y-m-d', $x);}

?>

Date function to display all dates between two dates

There is the DatePeriod class.

EXAMPLE:

$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
echo $date->format("Y-m-d") . "<br>";
}

(P1D stands for period of one day, see DateInterval for further documentation)

How to get all dates between two dates

<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");

function list_days($date_from,$date_to){
$arr_days = array();
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days

$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
//echo date("F j, Y \n", $day_to_display);
$arr_days[] = date('o-m-d',$day_to_display);
$counter++;
}

return $arr_days;
}

print_r(list_days($date_from,$date_to));
?>

PHP: List of days between two dates

$date1 = '29/08/2013';
$date2 = '03/09/2013';

function returnDates($fromdate, $todate) {
$fromdate = \DateTime::createFromFormat('d/m/Y', $fromdate);
$todate = \DateTime::createFromFormat('d/m/Y', $todate);
return new \DatePeriod(
$fromdate,
new \DateInterval('P1D'),
$todate->modify('+1 day')
);
}

$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
echo $date->format('d/m/Y'), PHP_EOL;
}

Get an array of all dates between 2 dates from every row in one array

You have to foreach every array to print the dates.

<?php
$result = mysqli_query($con, "SELECT * FROM invoice_line WHERE car_car_id = $id");
while ($auto = mysqli_fetch_array($result)) {?>
<h2 class="title_car">
<?php echo $auto['start_date'] . ' - ' . $auto['end_date'];?>
</h2>
<hr>
<?php
$date_text = "";
$dateRange = getDateRange($auto['start_date'], $auto['end_date']);
if(!empty($dateRange)){
foreach($dateRange as $dateR){
$date_text .= $dateR.", ";
}
}
echo rtrim($date_text,", ")."<br>";
}?>

Output:

2011-05-03, 2011-05-04, 2011-05-05, 2011-05-06, 2011-05-07, 2011-05-08

2011-05-20, 2011-05-21, 2011-05-22, 2011-05-23

.

.
.

How to get array of dates between two dates in laravel?

You can use CarbonPeriod for this.

I found something helpful at https://stackoverflow.com/a/50854594/13642447.

How to get dates between two dates

Try this PHP code:

<?php
$scheduleStartDate = '2015-06-20';
$scheduleEndDate = '2015-06-25';
$Date = getDatesFromRange($scheduleStartDate, $scheduleEndDate);
$Date = substr($Date, 0, -1);
function getDatesFromRange($start, $end){
$startDate = new DateTime($start);
$endDate = new DateTime($end);
$endDate->modify('+1 day');
$daterange = new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
$result = '';

foreach($daterange as $date){
$result .= '"'.$date->format("j-n-Y").'",';
}
return $result;
}
?>


Related Topics



Leave a reply



Submit