Skip to content

Instantly share code, notes, and snippets.

@tojibon
Created August 18, 2015 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tojibon/30b433d91ed59f8cb207 to your computer and use it in GitHub Desktop.
Save tojibon/30b433d91ed59f8cb207 to your computer and use it in GitHub Desktop.
Get an array of dates with Y-m-d format in between 2 input dates - PHP
<?php
/*
|- Returns an array of dates with Y-m-d format in between 2 input dates
|-
|- @params string $strDateFrom as date("Y-m-d") = "2014-01-01"
|- @params string $strDateTo as date("Y-m-d") = "2014-01-31"
|- @return array() of dates
|-
|- @author: Jewel Ahmed
|- @last modified: 18th Aug, 2015
*/
function getDateListsBetWeen2Dates($strDateFrom, $strDateTo)
{
$listOfDays = array();
$iDateFrom = mktime( 1, 0, 0, substr( $strDateFrom, 5, 2 ), substr( $strDateFrom, 8, 2 ), substr( $strDateFrom, 0, 4 ) );
$iDateTo = mktime( 1, 0, 0, substr( $strDateTo, 5, 2 ), substr( $strDateTo, 8, 2 ), substr( $strDateTo, 0, 4 ) );
if ( $iDateTo >= $iDateFrom )
{
$listOfDays[] = date( 'Y-m-d', $iDateFrom );
while ( $iDateFrom < $iDateTo )
{
$iDateFrom+= 86400; // add 24 hours
$listOfDays[] = date( 'Y-m-d', $iDateFrom );
}
}
return $listOfDays;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment