Adding a button for changing months

Discussion in 'PHP' started by genius, Jan 27, 2011.

  1. #1
    Hello,

    I have a calendar that I want to integrate into my website , but I want a button so the user can change the months.

    What line should I add ?

    I have the following code :

    <?php
    
    $date =time ();
    
    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('Y', $date);
    
    $first_day = mktime(0,0,0,$month, 1, $year);
    
    $title = date('F', $first_day);
    
    $day_of_week = date('D', $first_day);
    
    switch($day_of_week){
        case "Sun": $blank = 0; break;
        case "Mon": $blank = 1; break;
        case "Tue": $blank = 2; break;
        case "Wed": $blank = 3; break;
        case "Thu": $blank = 4; break;
        case "Fri": $blank = 5; break;
        case "Sat": $blank = 6; break;
    }
    
    $days_in_month = cal_days_in_month(0, $month, $year);
    
    echo "<table border=0 align=center id=calendar>";
    echo "<tr><th colspan=7> $title $year </th></tr>";
    echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
    
    $day_count = 1;
    
    echo "<tr>";
    
    while
    ( $blank > 0 )
    {
    echo "<td></td>";
    $blank = $blank-1;
    $day_count++;
    }
    
    $day_num = 1;
    
    while
    ($day_num <= $days_in_month)
    {
    
    if($day_num == $day)
    {
    echo "<td class=today>$day_num</td>";
    }
    else
    {
    echo "<td class=days>$day_num</td>";
    }
    
    
    $day_num++;
    $day_count++;
    
    if ($day_count > 7)
    {
    echo "</tr><tr>";
    $day_count = 1;
    }
    
    }
    
    while
    ($day_count >1 && $day_count <=7)
    {
    echo "<td> </td>";
    
    $day_count++;
    }
    
    echo "</tr></table>";
    
    ?>
    Code (markup):
     
    genius, Jan 27, 2011 IP
  2. GreenWithEnvy

    GreenWithEnvy Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Here's what I came up with:
    
    <?php
    $date = (!isset($_GET['date'])) ? time() : $_GET['date'];
    
    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('Y', $date);
    
    $first_day = mktime(0,0,0,$month, 1, $year);
    
    $title = date('F', $first_day);
    
    $day_of_week = date('D', $first_day);
    
    switch($day_of_week){
        case "Sun": $blank = 0; break;
        case "Mon": $blank = 1; break;
        case "Tue": $blank = 2; break;
        case "Wed": $blank = 3; break;
        case "Thu": $blank = 4; break;
        case "Fri": $blank = 5; break;
        case "Sat": $blank = 6; break;
    }
    
    $days_in_month = cal_days_in_month(0, $month, $year);
    
    $before_m = strtotime("-1 month", $first_day);
    $after_m = strtotime("+1 month", $first_day);
    echo "<table border=0 align=center id=calendar>";
    echo "<tr><th colspan=7>
    <!-- Prev month -->
    <form method=\"GET\" style=\"display:inline;\">
    <input type=\"hidden\" name=\"date\" value=\"$before_m\" />
    <input type=\"submit\" value=\"<-\" />
    </form>
     $title $year
    <!-- Next MONTH -->
    <form method=\"GET\" style=\"display:inline;\">
    <input type=\"hidden\" name=\"date\" value=\"$after_m\" />
    <input type=\"submit\" value=\"->\" />
    </form>
     </th></tr>";
    echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
    
    $day_count = 1;
    
    echo "<tr>";
    
    while
    ( $blank > 0 )
    {
    echo "<td></td>";
    $blank = $blank-1;
    $day_count++;
    }
    
    $day_num = 1;
    
    while
    ($day_num <= $days_in_month)
    {
    
    if($day_num == $day)
    {
    echo "<td class=today>$day_num</td>";
    }
    else
    {
    echo "<td class=days>$day_num</td>";
    }
    
    
    $day_num++;
    $day_count++;
    
    if ($day_count > 7)
    {
    echo "</tr><tr>";
    $day_count = 1;
    }
    
    }
    
    while
    ($day_count >1 && $day_count <=7)
    {
    echo "<td> </td>";
    
    $day_count++;
    }
    
    echo "</tr></table>";
    
    ?>
    
    PHP:
     
    GreenWithEnvy, Jan 27, 2011 IP
  3. genius

    genius Well-Known Member

    Messages:
    535
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #3
    It;s great , but I also need the week number in front , can you help me with that ?

    Thank you very much.
     
    genius, Jan 28, 2011 IP
  4. GreenWithEnvy

    GreenWithEnvy Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Care to elaborate?

    http://php.net/manual/en/function.date.php

    Can help you with a lot.

    if you want to echo the week number, you can set at the top next to month/year $week = date('W', $date); and then echo $week whereever you want.
     
    GreenWithEnvy, Jan 28, 2011 IP
  5. genius

    genius Well-Known Member

    Messages:
    535
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Can someone please help me ? I really need this script and can't figure it out ...
     
    genius, Jan 28, 2011 IP
  6. genius

    genius Well-Known Member

    Messages:
    535
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #6
    I just can't seem to make it work , I've set

    $day = date('d', $date);
    $month = date('m', $date);
    $week = date('W', $date);
    $year = date('Y', $date);

    and now i can make it work , i wnt to week number to be displayed in front of the days ...
     
    genius, Jan 29, 2011 IP
  7. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #7
    so you can do

    echo $week." - ".$day;
     
    G3n3s!s, Jan 29, 2011 IP
  8. genius

    genius Well-Known Member

    Messages:
    535
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #8
    And where should I write "echo $week." - ".$day"
     
    genius, Jan 30, 2011 IP
  9. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #9
    you told
    So I wrote how it should be done
     
    G3n3s!s, Jan 30, 2011 IP