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,742
    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,742
    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,742

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.17486 seconds
  • Memory Usage 2,957KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates