www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] How can I find every day in a date span?

  1. #1
    Join Date
    Dec 2004
    Posts
    439

    resolved [RESOLVED] How can I find every day in a date span?

    Hello.

    I have a date span, like 90 days ago till today, is 2006-11-12 to 2007-02-10, and I need to know every date in the span, like below:

    2006-11-12
    2006-11-13
    2006-11-14
    2006-11-15
    2006-11-16
    etc.

    any help would be appreciated much, thanks

  2. #2
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    PHP Code:
    <?php
    $fr_yr 
    2006;
    $fr_mo 11;
    $fr_dy 12;
    $date_span 90;

    $arrDATES = array();
    FOR (
    $x 0$x $date_span$x++) :
      
    $arrDATE[] = date("Y-m-d"mktime(0,0,0,$fr_mo$fr_dy+$x$fr_yr););
    ENDFOR;

    print 
    "<pre>";
    print_r($arrDATES);
    print 
    "</pre>";
    ?>

  3. #3
    Join Date
    Dec 2004
    Posts
    439
    Thanks man, this works! I really appreciate it

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    I wrote this last night but never got around to posting it. It's a slightly different take on it. It takes the start and end dates and I did it like this because I thought your source dates were in this format, YYYY-mm-dd.
    PHP Code:
    <?php

    function GetDates($start$end)
    {
        if(
    preg_match('/^(\d{4})-(\d{2})-(\d{2})$/'$start$match1)
        and (
    $time1 mktime(000$match1[2], $match1[3], $match1[1]))
        and 
    preg_match('/^(\d{4})-(\d{2})-(\d{2})$/'$end$match2)
        and (
    $time2 mktime(000$match2[2], $match2[3], $match2[1])))
        {
            while(
    $time2 >= $time1)
            {
                
    $rtn[] = date('Y-m-d'$time1);
                
    $time1 += 86400;
            }
            return 
    $rtn;
        }
        return 
    false;
    }

    print_r(GetDates('2005-06-02''2006-06-02'));

    ?>

  5. #5
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by bokeh
    I wrote this last night but never got around to posting it. It's a slightly different take on it. It takes the start and end dates and I did it like this because I thought your source dates were in this format, YYYY-mm-dd.
    PHP Code:
    <?php

    function GetDates($start$end)
    {
        if(
    preg_match('/^(\d{4})-(\d{2})-(\d{2})$/'$start$match1)
        and (
    $time1 mktime(000$match1[2], $match1[3], $match1[1]))
        and 
    preg_match('/^(\d{4})-(\d{2})-(\d{2})$/'$end$match2)
        and (
    $time2 mktime(000$match2[2], $match2[3], $match2[1])))
        {
            while(
    $time2 >= $time1)
            {
                
    $rtn[] = date('Y-m-d'$time1);
                
    $time1 += 86400;
            }
            return 
    $rtn;
        }
        return 
    false;
    }

    print_r(GetDates('2005-06-02''2006-06-02'));

    ?>
    Or this:
    PHP Code:
    function GetDates($start$end)
    {
        
    $rtn false;
        if( (
    $time1 strtotime($start)) AND ($time2 strtotime($end) );
        {
            
    $rtn = array();
            while(
    $time2 >= $time1)
            {
                
    $rtn[] = date('Y-m-d'$time1);
                
    $time1 += 86400;
            }
        }
        return 
    $rtn;


  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Yeah, you could do that, (I was considering that to start with) but I did it this way so I could check the input was formated correctly.

  7. #7
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    I think that strtotime() does that. It returns false if it can't sort things out or a timestamp. That would give your function more "flexibility" - although that could be a two-edged sword...

    Also, the function now has a single entry and exit point.

  8. #8
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    ┐two-edged or double-edged?

    Voy a acabar mal...

  9. #9
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles