Return all dates between two dates in an array in PHP
Given two dates (start date and end date) and the task is to return all the dates in an array.
Example 1: In this example, use date interval class which stores fixed amount of time (in years, months, days, hours etc) or relative time string in the format that DateTime.
<?php // Function to get all the dates in given range function getDatesFromRange( $start , $end , $format = 'Y-m-d' ) { // Declare an empty array $array = array (); // Variable that store the date interval // of period 1 day $interval = new DateInterval( 'P1D' ); $realEnd = new DateTime( $end ); $realEnd ->add( $interval ); $period = new DatePeriod( new DateTime( $start ), $interval , $realEnd ); // Use loop to store date into array foreach ( $period as $date ) { $array [] = $date ->format( $format ); } // Return the array elements return $array ; } // Function call with passing the start date and end date $Date = getDatesFromRange( '2010-10-01' , '2010-10-05' ); var_dump( $Date ); ?> |
array(5) { [0]=> string(10) "2010-10-01" [1]=> string(10) "2010-10-02" [2]=> string(10) "2010-10-03" [3]=> string(10) "2010-10-04" [4]=> string(10) "2010-10-05" }
Example 2: This example use strtotime() function which is used to convert an English textual date-time description to a UNIX timestamp. It returns a timestamp on success, False otherwise.
<?php // Declare two dates $Date1 = '01-10-2010' ; $Date2 = '05-10-2010' ; // Declare an empty array $array = array (); // Use strtotime function $Variable1 = strtotime ( $Date1 ); $Variable2 = strtotime ( $Date2 ); // Use for loop to store dates into array // 86400 sec = 24 hrs = 60*60*24 = 1 day for ( $currentDate = $Variable1 ; $currentDate <= $Variable2 ; $currentDate += (86400)) { $Store = date ( 'Y-m-d' , $currentDate ); $array [] = $Store ; } // Display the dates in array format print_r( $array ); ?> |
Array ( [0] => 2010-10-01 [1] => 2010-10-02 [2] => 2010-10-03 [3] => 2010-10-04 [4] => 2010-10-05 )
Recommended Posts:
- How to store all dates in an array present in between given two dates in JavaScript ?
- Sort an array of dates in PHP
- How to select Min/Max dates in an array using JavaScript ?
- Comparing two dates in PHP
- Compare Dates in Java
- Compare two dates using JavaScript
- How to calculate the difference between two dates in PHP?
- PHP | Number of week days between two dates
- How to calculate minutes between two dates in JavaScript ?
- Program to find the number of days between two dates in PHP
- How to calculate the number of days between two dates in javascript?
- What does main() return in C and C++?
- How to return multiple values from function in PHP?
- When to use PreventDefault( ) vs Return false in JavaScript?
- JavaScript | Return multiple values from function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.