www.webdeveloper.com
Results 1 to 4 of 4

Thread: how to accomplish this ?

  1. #1
    Join Date
    Feb 2007
    Posts
    84

    how to accomplish this ?

    Hello forums!!
    Suppose consider the following case:
    I had the following date range:
    From: 2006-05-10
    To: 2007-08-15
    Q's?
    1> How to find the no of saturdays within the range with appropriate date format ie YYYY-mm-dd .

    2> How to find the first and last saturday of each month within the range ?

    Thanks in advance for the kind help.

  2. #2
    Join Date
    Apr 2005
    Location
    Bathurst, NSW, Australia
    Posts
    3,357
    So us what you have attempted for your assignment/homework and we may be able to assist you, we won't, however, just provide you with an answer.

  3. #3
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Yeah, I have totally given up on helping this guy, he never replies after people have gone to the trouble of helping him.

  4. #4
    Join Date
    Feb 2007
    Posts
    84
    I have found this and its great.. really works superbly.
    PHP Code:
    <?php
    // convert to timestamp
    $start strtotime'2006-05-10 00:00:00');
    $end strtotime'2007-08-15 00:00:00');

    // initialize an array to hold the saturdays
    $saturdays = array();
    $monthSaturdays = array();

    // this is for question 1
    for ( $i $start$i  <= $end$i += 86400 ) {
       if ( (int)
    date'w'$i ) == ) {
            
    // store the saturdays
            
    $saturdays[] = date'Y-m-d'$i );
        }
    }

    // this is for question 2
    for ( $i $start$i <= $end$i += 86400 ) {
        
    $currentMonth date'Y-m'$i );
        if ( ! isset( 
    $monthSaturdays[$currentMonth] ) ) {
            
    $monthSaturdays[$currentMonth] = array( nullnull );
        }
        
    // store the first Saturday of the month (index 0 will hold the firs, index 1 will hold the last)
        
    if ( ! isset( $monthSaturdays[$currentMonth][0] ) && (int)date'w'$i ) == ) {
            
    $monthSaturdays[$currentMonth][0] = date'Y-m-d'$i );   
        }
        
    // the last Saturday will be automatically set
        
    if ( (int)date'w'$i ) == ) {
            
    $monthSaturdays[$currentMonth][1] = date'Y-m-d'$i ); 
        }
    }

    ?>

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