SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Create Dates array

    I've been using this function to build an array of dates from a "FromDate" to a "ToDate" and just noticed the results are not always correct. I'm hoping someone can point out why or a more reliable way to build this array. You'll notice some commented out sample dates and the result. Thank you for your expertise.
    PHP Code:
    <?php
        
    function createDateRangeArray($strDateFrom,$strDateTo){

            
    $aryRange=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)
            {
                
    array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
                
    while ($iDateFrom<$iDateTo)
                {
                    
    $iDateFrom+=86400// add 24 hours
                    
    array_push($aryRange,date('Y-m-d',$iDateFrom));
                }
            }
            return 
    $aryRange;
        }
    //Incorrect
    //$FromDate = "2013-11-01";
    //$ToDate = "2013-11-30";    

    //Correct
    //$FromDate = "2013-11-01";
    //$ToDate = "2013-11-15";

    //Correct
    //$FromDate = "2013-12-01";
    //$ToDate = "2013-12-30"; 

    //Correct
    //$FromDate = "2013-11-15";
    //$ToDate = "2013-12-15";

    //Correct
    //$FromDate = "2013-11-15";
    //$ToDate = "2013-12-30";

    //Incorrect
    $FromDate "2013-11-01";
    $ToDate "2013-12-30";

    $dates_array createDateRangeArray($FromDate,$ToDate);

    echo 
    "$FromDate<br />$ToDate<br />";        
    echo 
    "<pre>";
    print_r($dates_array);
    echo 
    "</pre>";          
    ?>

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Well I changed it to strtotime() and it seems to be working correctly.

    PHP Code:
    <?php
        
    function createDateRangeArray($strDateFrom,$strDateTo){

            
    $aryRange=array();
            
    $iDateFrom strtotime($strDateFrom);
            
    $iDateTo strtotime($strDateTo);
        
            if (
    $iDateTo>=$iDateFrom)
            {
                
    array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
                
    while ($iDateFrom<$iDateTo)
                {
                    
    $iDateFrom+=86400// add 24 hours
                    
    array_push($aryRange,date('Y-m-d',$iDateFrom));
                }
            }
            return 
    $aryRange;
        }
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •