Only Show Saturdays of the month

Discussion in 'PHP' started by Raygo, May 15, 2010.

  1. #1
    Hello. I want to show all the saturdays of this month, I tried the following code but its not working, can somebody help me?

    
    $dtFirstDay = date("j/n/Y", mktime(0, 0, 0, date("m") , date("d")-date("d")+1, date("Y")));
    
    $dtLastDay = date("j/n/Y", mktime(0, 0, 0, date("m")+1 , date("d")-date("d"), date("Y"))); 
    
        for($date = $dbFirstDay ; $date < $dtLastDay; $date = mktime(0,0,0,date("m", $date),date("d", $date)+1,date("Y", $date))){
            if(date('w' ,$date) == 0){
                echo $date;
            }
    
            }
    
    
    PHP:
     
    Raygo, May 15, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    <?php
    
    $total_days = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')); 
    echo "Their are ".round($total_days / 7)." Saturdays within ".date('M, Y');
    
    ?>
    PHP:
     
    danx10, May 15, 2010 IP
  3. Raygo

    Raygo Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats not really what i want to show, I want to show the date for every saturday of the month
     
    Raygo, May 15, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    <?php
    //$total_days = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
    $total_days = date('t',strtotime('today'));
    
    $day_nums = range(1, $total_days);
    
    $dates = array();
    $i = 0;
    foreach($day_nums as $num){
    $i++;
    $dates[$i] = $num.' '.date('M').' '.date('Y');
    }
    
    $strdates = array();
    $i = 0;
    foreach($dates as $date){
    $i++;
    $strdates[$i] = strtotime($date);
    }
    
    $saturdays = array();
    $i = 0;
    foreach($strdates as $strdate){
    $strdate_info = getdate($strdate);
    if ($strdate_info['weekday'] == "Saturday"){
    $i++;
    $saturdays[$i] = $strdate_info['mday'].' '.date('M').' '.date('Y');
    }
    }
    
    //returns an array of saturdays with their date numbers...
    print_r($saturdays);
    
    ?>
    PHP:
    Could have been done another way, but this is what I could think of.
     
    danx10, May 15, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    I had a little look around too... very basic but you might find it useful, it will echo out the next 4 Saturdays

    
    <?php
    echo date('l jS F (Y-m-d)', strtotime('this Saturday'));
    echo '<br />';
    echo date('l jS F (Y-m-d)', strtotime('next Saturday'));
    echo '<br />';
    echo date('l jS F (Y-m-d)', strtotime('third Saturday'));
    echo '<br />';
    echo date('l jS F (Y-m-d)', strtotime('fourth Saturday'));
    ?> 
    
    PHP:
    Equals:
    Saturday 15th May (2010-05-15)
    Saturday 22nd May (2010-05-22)
    Saturday 5th June (2010-06-05)
    Saturday 12th June (2010-06-12)

    or this

    
    <?php
    echo date('l jS F', strtotime('this Saturday'));
    echo '<br />';
    echo date('l jS F', strtotime('next Saturday'));
    echo '<br />';
    echo date('l jS F', strtotime('third Saturday'));
    echo '<br />';
    echo date('l jS F', strtotime('fourth Saturday'));
    ?>
    
    PHP:
    Equals:
    Saturday 15th May
    Saturday 22nd May
    Saturday 5th June
    Saturday 12th June
     
    Last edited: May 15, 2010
    MyVodaFone, May 15, 2010 IP