Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date function to display all dates between two dates

Tags:

date

php

Is there any PHP function to display all dates between two dates?

like image 357
Kathiravan Avatar asked Feb 13 '13 10:02

Kathiravan


People also ask

How do I get all the dates between two dates in Python?

import pandas from datetime import datetime, timedelta startDate = datetime(2022, 6, 1) endDate = datetime(2022, 6, 10) # Getting List of Days using pandas datesRange = pandas. date_range(startDate,endDate-timedelta(days=1),freq='d') print(datesRange);

Is there a between dates function in Excel?

Use the DATEDIF function when you want to calculate the difference between two dates. First put a start date in a cell, and an end date in another.

How do I create an array between two dates in Excel?

Sequence() Function Since Excel dates are whole numbers, all you need to do is ask Sequence() to insert an array from a Start Date to an End Date. Sequence(1 + EndDate – StartDate, 1, StartDate) the formula will spill the dates into the cells below.


3 Answers

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)

like image 148
Fabian Schmengler Avatar answered Oct 21 '22 23:10

Fabian Schmengler


You can check out this function also

        $day = 86400; // Day in seconds  
        $format = 'Y-m-d'; // Output format (see PHP date funciton)  
        $sTime = strtotime($start_date); // Start as time  
        $eTime = strtotime($end_date); // End as time  
        $numDays = round(($eTime - $sTime) / $day) + 1;  
        $days = array();  

        for ($d = 0; $d < $numDays; $d++) {  
            $days[] = date($format, ($sTime + ($d * $day)));  
        }  
like image 44
Venkata Krishna Avatar answered Oct 21 '22 21:10

Venkata Krishna


    $start_date  = '2020/09/01';
    $end_date    = '2020-09-03';

    $new_date = new DateTime($end_date);
    $new_date->add(new DateInterval('P1D'));
    $end_date = $new_date->format('Y-m-d');

    $period = new DatePeriod(
    new DateTime($start_date),
    new DateInterval('P1D'),
    new DateTime($end_date)
   );

   print_r($period);
   foreach ($period as $key => $value) {
     echo $value->format('Y-m-d')  .'<br>';      
   }    

Print:
2020-09-01
2020-09-02
2020-09-03

like image 36
Mahesh Kathiriya Avatar answered Oct 21 '22 23:10

Mahesh Kathiriya