Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: createWeek function, messes up in ONE specific week

  1. #1

    createWeek function, messes up in ONE specific week

    I'm losing hair over this I have the following function. The parameter accepts a date, then the function generates a table with the days of the week for that date. But when you submit the present week, it displays two Sundays, instead of one. I'm not too experienced handling dates and times. Can anyone please help me?
    PHP Code:
    function createWeek($enterDate)
        {
            echo 
    '<h1>'.date('l'strtotime($enterDate)).'</h1>';
            if(
    date('l'strtotime($enterDate))=='Sunday')
            {
                echo 
    '<h2>its a sunday</h2>';
                
    $startDate date('r',strtotime($enterDate));
                
    $endDate date('r',strtotime('next Saturday '.$enterDate));
            }
            elseif(
    date('l'strtotime($enterDate))=='Saturday')
            {
                echo 
    '<h2>its a saturday</h2>';
                
    $startDate date('r',strtotime('last Sunday '.$enterDate));
                
    $endDate date('r',strtotime('this Saturday '.$enterDate));
            }
            else
            {
                echo 
    '<h2>not saturday nor sunday</h2>';
                
    $startDate date('r',strtotime('last Sunday '.$enterDate));
                
    $endDate date('r',strtotime('next Saturday '.$enterDate));
            }
    //        echo '<pre>';
    //        print_r(getdate(strtotime($enterDate)));
    //        echo '</pre>';
    //        echo 'Date: '.date('l F j, Y', strtotime($enterDate)).'<br />';
    //        echo date('W', strtotime($enterDate)).'th week<br />';
    //        echo 'Start day: '.$startDate;
    //        echo '<br />';
    //        echo 'End day: '.$endDate.'<br />';
            
            
    $weekDaysLabel '<tr class="daysOfWeek"><td>'.date('Y'strtotime($startDate)).'</td>';
            
    $daysTime '<tr>';
        
            
    // cells for each day of the week
            
    for($i=0;$i<7;$i++)
            {
                
    $weekDaysLabel .= '<td>'.date('l, M j'strtotime('+'.$i.' day '.$startDate)).' | +'.$i.' day '.$startDate.'</td>';
            }
        
            
    // tr's, rows for each time
            
    $time = array('6 AM''7 AM''8 AM''9 AM''10 AM''11 AM''NOON''1 PM''2 PM''3 PM''4 PM''5 PM''6 PM''7 PM''8 PM''9 PM''10 PM');
            
            foreach(
    $time as $key => $value)
            {
                
    $times .= '<tr><td>'.$value.'</td>';
                
                for(
    $a=1;$a<8;$a++)
                {
                    
    $times .= '<td></td>';
                }
                
                
    $times .= '</tr>';
            }
            
            
    $daysTime .= '</tr>';
            
    $weekDaysLabel .= '</tr>';

            
    $weekHeader =    '<p>
                                <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&CurrentMonth='.date('m/d/Y'strtotime('-1 day '.$startDate)).'">Previous Week</a>
                                 | 
                                <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&CurrentMonth='.date('m/d/Y'strtotime('+8 days '.$startDate)).'">Next Week</a>
                            </p>'
    ;

            return 
    $weekHeader.'<table id="LeoCalendar">'.$weekDaysLabel.$daysTime.$times.'</table>';
        } 
    Last edited by imagined; November 3rd, 2009 at 01:02 PM.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  2. #2
    A bit improved function
    PHP Code:
    function createWeek($enterDate)
        {
            echo 
    '<h1>'.date('l'strtotime($enterDate)).'</h1>';
            if(
    date('l'strtotime($enterDate))=='Sunday')
            {
                echo 
    '<h2>its a sunday</h2>';
                
    $startDate date('r',strtotime($enterDate));
                
    $endDate date('r',strtotime('next Saturday '.$enterDate));
            }
            elseif(
    date('l'strtotime($enterDate))=='Saturday')
            {
                echo 
    '<h2>its a saturday</h2>';
                
    $startDate date('r',strtotime('last Sunday '.$enterDate));
                
    $endDate date('r',strtotime('this Saturday '.$enterDate));
            }
            else
            {
                echo 
    '<h2>not saturday nor sunday</h2>';
                
    $startDate date('r',strtotime('last Sunday '.$enterDate));
                
    $endDate date('r',strtotime('next Saturday '.$enterDate));
            }

            
    // ---------- cells for each day of the week ----------
            
    $weekDaysLabel '    <colgroup>
                                    <col id="year" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr class="daysOfWeek"><th scope="col">'
    .date('Y'strtotime($startDate)).'</th>';
            for(
    $i=0;$i<7;$i++)
            {
                
    $weekDaysLabel .= '<th scope="col">'.date('l, M j'strtotime('+'.$i.' day '.$startDate)).' | +'.$i.' day '.$startDate.'</th>';
            }
            
    $weekDaysLabel .= '</tr></thead>';
        
            
    // ---------- tr's, rows for each time ----------
            
    $time = array('6 AM''7 AM''8 AM''9 AM''10 AM''11 AM''NOON''1 PM''2 PM''3 PM''4 PM''5 PM''6 PM''7 PM''8 PM''9 PM''10 PM');
            
    $daysOfWeek = array('Sunday''Monday''Tuesday''Wednesday''Thursday''Friday''Saturday');
            foreach(
    $time as $key => $value)
            {
                
    $times .= '<tr><td>'.$value.'</td>';
                foreach(
    $daysOfWeek as $key_dow => $value_dow)
                {
                    
    $times .= '<td></td>';
                }
                
                
    $times .= '</tr>';
            }
            
            
    $weekHeader =    '<p>
                                <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&CurrentMonth='.date('m/d/Y'strtotime('-1 day '.$startDate)).'">Previous Week</a>
                                 | 
                                <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&CurrentMonth='.date('m/d/Y'strtotime('+8 days '.$startDate)).'">Next Week</a>
                            </p>'
    ;
            return 
    $weekHeader.'<table id="LeoCalendar">'.$weekDaysLabel.$times.'</table>';
        } 
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  3. #3
    Hrm ... I would think that you should be able to handle this without all that $startdate $enddate jazz, as well as not really needing to define any arrays like the $time and the $daysOfWeek array.
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  4. #4
    The function is supposed to create a table with the days of the week and times for each day. Just like a calendar in week view. Similar to this: http://www.spicebird.com/content/calendar-week-view

    The $startDate is supposed to get the Sunday for that week. The $endDate is supposed to get the Saturday for that week. Then a table with those weekdays is populated.

    It seems to work fine, the problem is, when you use THIS current week, it shows two Sundays
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  5. #5
    try this:
    PHP Code:
    <?php
    function createWeek($date) {

        
    // Setup
        
    $timestamp strtotime($date);

        
    $week date('W'$timestamp);
        
    $year date('Y'$timestamp);
        
        
    $sunday strtotime($year '-W' $week '-0');
        
    $legible_date date("F d, Y"$sunday);
        
        
    // Output
        
    $output '';
        
    $output .= '<table><colgroup><col id="year" /><col id="Sunday" /><col id="Monday" /><col id="Tuesday" /><col id="Wednesday" /><col id="Thursday" /><col id="Friday" /><col id="Saturday" /></colgroup><thead><tr><th><h1>' $year '</h1></th>';

        for (
    $i 0$i 7$i++):
            
    $date date('l, F d, Y'strtotime("+$i day $legible_date"));
            
    $date = ( strtotime($date) === $timestamp ) ? '<span>' $date '</span>' $date
            
            
    $output .= '<th scope="col">' $date '</th>';
        endfor;

        
    $output .= '</tr></thead>';
        
    $output .= '<tbody>';
        
        for (
    $i 0$i <= 16$i++):
            
    $time strtotime("+$i hour 6AM");
            
            
    $hour date('H'$time);
            
    $time = ( $hour != '12' ) ? date('g A'$time) : 'Noon' ;
        
            
    $output .= '<tr><th scope="row">' $time '</th>';
            
                for (
    $j 1$j <= 7$j++):
                    
    $output .= '<td></td>';
                endfor;
            
            
    $output .= '</tr>';
        endfor;
                
        
    $output .= '</tbody></table>';
        
        return 
    $output;
    }
    ?>
    PHP Code:
    createWeek('November 3 2009'); 
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	cal.gif 
Views:	13 
Size:	19.0 KB 
ID:	51086  
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  6. #6
    You the best man! You simply the best!!! Awesome! Thanks!
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  7. #7
    Just when you enter a Sunday, it gives you the previous week, instead of that Sunday's week. I tried with Nov 15, and Nov 8

    I will take a look at it tomorrow because I gotta go.

    Thanks so much!
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  8. #8
    Must be an error in the logic due to the fact that the beginning of the week is technically Monday. If you make it so it shows as M, T, W, R, F, S, Su instead of Su, M, T, W, R, F, S it will work properly. Which would be changing:

    PHP Code:
    strtotime($year '-W' $week '-0'
    to
    PHP Code:
    strtotime($year '-W' $week '-1'
    You'll have to do some extra logic for it to work as Sunday being the first day. This would do the trick I suppose:
    PHP Code:
    $sunday = ( date('w'$timestamp) == ) ? strtotime($year '-W' . ($week 1) . '-0') : strtotime($year '-W' $week '-0'); 
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  9. #9
    Thanks, it does work like that with the week starting Sunday. I'm trying to figure out why, but when you enter Jan 1, 2 or 3rd of 2010, it doesn't work.

    For example, if you try to print out the $legible_date at the top of the page when you enter 01/03/2010 in the function, $legible_date would be December 31, 1969

    and, $date=01/03/2010, then legible date would be Dec 31, 1969 too
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  10. #10
    I was using the following date format m/d/Y at the $_GET['workDate'] , but I'm going to change it to MySQL style Y-m-d to avoid some conversions and clarify code.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  11. #11
    I actually just looked at the function a little more closely and tested it and actually the
    PHP Code:
    $sunday = ( date('w'$timestamp) == ) ? strtotime($year '-W' . ($week 1) . '-0') : strtotime($year '-W' $week '-0'); 
    is incorrect, I echoed the Week number and it's actually on the incorrect week. Lemme see what I can work out
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  12. #12
    What do you think of this? I think it works now.

    Also, I tweaked the code a little bit on the hours column. I changed the for loop so the value of $i can match the time in 24-hour format in case some calculations need to be added later. That way, it would be easier to notice the logic.

    PHP Code:
        function createWeek($enterDate)
        {
            
    $today date('Y-m-d');
            
    $today_timestamp strtotime($today);

            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            if(
    date('l'$timestamp)=='Sunday')
            {
                
    $sunday_timestamp $timestamp;
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            else
            {
                
    $sunday_timestamp strtotime(date('r',strtotime('last Sunday '.$timestamp_legible)));
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }

            
    $year date('Y'$sunday_timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">Previous Week</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">Next Week</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr class="daysOfWeek"><th scope="col"><h1>'
    .$year.'</h1></th>
                        '
    ;

            for(
    $i=0;$i<7;$i++)
            {
                
    $date date('l, M j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $today_col '';
                if(
    strtotime($date) === $today_timestamp)
                {
                    
    $today_col 'id="today_col"';
                }
                else
                {
                    
    $today_col '';
                }

                
    $output .= '<th scope="col" '.$today_col.'>'.$date.'</th>';
            }
            
    $output .= '</tr></thead><tbody>';
        
            for(
    $i=6$i <= 22$i++)
            {
                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $output .= '<tr><th scope="row">'.$time.'</th>';
                
                for(
    $j=1$j<=7;$j++)
                {
                    
    $output .= '<td></td>';
                }

                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';

            return 
    $output;
        } 
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  13. #13
    cool if that works , here's what I fiddled with. I added some extra more logic into it which is why it's longer:
    PHP Code:
    <?php
    <?php
    function createWeek($date$sunday_first true) {

        
    $timestamp = ( !is_numeric($date) ) ? strtotime($date) : $date;
        
    $timestamp htmlentities($timestamp);

        if ( !empty(
    $timestamp) ):
            
    // Setup
            
    $week date('W'$timestamp);
            
    $year date('Y'$timestamp);
            
    $current_date date('F d, Y'time());

            
    $first_day strtotime($year '-W' $week '-1');
            
    $legible_date date('F d, Y'$first_day);

            
    $next_week strtotime('+1 weeks ' $legible_date);
            
    $previous_week strtotime('-1 weeks ' $legible_date);
            
    $today = ( $sunday_first ) ? strtotime('-1 weeks ' $current_date) : time() ;

            for (
    $i 0$i 7$i++):
                
    $date date('l, F d, Y'strtotime("+$i day $legible_date"));
                
    $dates[] = ( $i != && $sunday_first ) ? date('l, F d, Y'strtotime("+1 weeks $date")) : $date;
            endfor;

            
    // Move Sunday to beginning of the week
            
    if ( $sunday_first ):
                
    $sunday array_pop($dates);
                
    array_unshift($dates$sunday); 
            endif;

            
    // Dynamic colgroup
            
    foreach ( $dates as $date ):
                
    $col .= '<col id="' date('l'strtotime($date)) . '" />';
            endforeach;

            
    // Output
            
    $output '';
            
    $output .= '<h2>' date('l, F d, Y'$first_day) . ' &ndash; ' date('l, F d, Y'strtotime('+6 days '.date('l, F d, Y'$first_day))) . '</h2>';
            
    $output .= '<a href="' basename($_SERVER['PHP_SELF']) . '?t=' $today .'">Today</a> | <a href="' basename($_SERVER['PHP_SELF']) . '?t=' $previous_week .'">&#x2190; Previous Week</a> | <a href="' basename($_SERVER['PHP_SELF']) . '?t=' $next_week .'">Next Week &#x2192;</a>';
            
    $output .= '<table><colgroup><col id="year" />';
            
    $output .= $col;
            
    $output .= '</colgroup><thead>';
            
    $output .= '<tr><th><h1>' $year '</h1></th>';

            foreach ( 
    $dates as $date ):
                
    $date = ( strtotime($date.' 00:00:00') == strtotime(date('M d Y 00:00:00',time())) ) ? '<span>' $date '</span>' $date;
                
    $output .= '<th scope="col">' $date '</th>';
            endforeach;

            
    $output .= '</tr></thead>';
            
    $output .= '<tbody>';

            
    // Create times
            
    for ($i 0$i <= 16$i++):
                
    $time strtotime("+$i hour 6AM");

                
    $hour date('H'$time);
                
    $time = ( $hour != '12' ) ? date('g A'$time) : 'Noon' ;

                
    $output .= '<tr><th scope="row">' $time '</th>';

                    for (
    $j 1$j <= 7$j++):
                        
    $output .= '<td></td>';
                    endfor;

                
    $output .= '</tr>';
            endfor;

            
    $output .= '</tbody></table>';
        else:
            
    $output '<h1>Invalid Date</h1>';
        endif;

        return 
    $output;
    }
    ?>
    it allows you to switch between monday and sunday as the start day, I verified the output w/ the same output that iCal gives if you have monday selected or sunday selected as your start day. i also put a in the "next" "previous" as well as "today" links. oh it also highlights the current day this is what i did to test it:
    PHP Code:
    <?php
        $date 
    = ( !empty($_GET['t']) ) ? $_GET['t'] : '01/03/2010' ;
        echo 
    createWeek($date);
    ?>
    the x - y date display doesn't have the correct logic applied to it as I only messed w/ it while i was tweaking w/ monday as the start
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  14. #14
    updated ... should work fine now ... i forgot to take into consideration the input date. tis my first time trying to make a calendar type thingy
    PHP Code:
    <?php
    function createWeek($date$sunday_first true) {

        
    $timestamp = ( !is_numeric($date) ) ? strtotime($date) : $date;
        
    $timestamp htmlentities($timestamp);

        if ( !empty(
    $timestamp) ):
            
    // Setup
            
    $week date('W'$timestamp);
            
    $year date('Y'$timestamp);
            
    $current_date date('F d, Y'time());

            if ( 
    $sunday_first ):
                
    $first_day = ( date('w'$timestamp) == ) ? strtotime($year '-W' . ($week 1) . '-1') : strtotime($year '-W' $week '-1');
            else:
                
    $first_day strtotime($year '-W' $week '-1');
            endif;

            
    $legible_date date('F d, Y'$first_day);

            
    $next_week strtotime('+1 weeks ' $legible_date);
            
    $previous_week strtotime('-1 weeks ' $legible_date);
            
    $today time() ;

            for (
    $i 0$i 7$i++):
                
    $date date('l, F d, Y'strtotime("+$i day $legible_date"));
                if ( 
    $sunday_first ):
                    
    $dates[] = ( $i != ) ? $date date('l, F d, Y'strtotime("-1 weeks $date"));
                else:
                    
    $dates[] = $date;
                endif;
            endfor;

            
    // Move Sunday to beginning of the week
            
    if ( $sunday_first ):
                
    $sunday array_pop($dates);
                
    array_unshift($dates$sunday); 
            endif;

            
    // Dynamic colgroup
            
    foreach ( $dates as $date ):
                
    $col .= '<col id="' date('l'strtotime($date)) . '" />';
            endforeach;

            
    $header = ( $sunday_first ) ? date('l, F d, Y'strtotime('-1 days '.date('l, F d, Y'$first_day)))  . ' &ndash; ' date('l, F d, Y'strtotime('+5 days '.date('l, F d, Y'$first_day))) : date('l, F d, Y'$first_day) . ' &ndash; ' date('l, F d, Y'strtotime('+6 days '.date('l, F d, Y'$first_day))) ;

            
    // Output
            
    $output '';
            
    $output .= '<h2>' $header '</h2>';
            
    $output .= '<a href="' basename($_SERVER['PHP_SELF']) . '?t=' $today .'">Today</a> | <a href="' basename($_SERVER['PHP_SELF']) . '?t=' $previous_week .'">&#x2190; Previous Week</a> | <a href="' basename($_SERVER['PHP_SELF']) . '?t=' $next_week .'">Next Week &#x2192;</a>';
            
    $output .= '<table><colgroup><col id="year" />';
            
    $output .= $col;
            
    $output .= '</colgroup><thead>';
            
    $output .= '<tr><th><h1>' $year '</h1></th>';

            foreach ( 
    $dates as $date ):
                
    $date = ( strtotime($date.' 00:00:00') == strtotime(date('M d Y 00:00:00',time())) ) ? '<span>' $date '</span>' $date;
                
    $output .= '<th scope="col">' $date '</th>';
            endforeach;

            
    $output .= '</tr></thead>';
            
    $output .= '<tbody>';

            
    // Create times
            
    for ($i 0$i <= 16$i++):
                
    $time strtotime("+$i hour 6AM");

                
    $hour date('H'$time);
                
    $time = ( $hour != '12' ) ? date('g A'$time) : 'Noon' ;

                
    $output .= '<tr><th scope="row">' $time '</th>';

                    for (
    $j 1$j <= 7$j++):
                        
    $output .= '<td></td>';
                    endfor;

                
    $output .= '</tr>';
            endfor;

            
    $output .= '</tbody></table>';
        else:
            
    $output '<h1>Invalid Date</h1>';
        endif;

        return 
    $output;
    }
    ?>
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  15. #15
    Awesome man! It works great. It's my first time also working with a calendar thing. I'm creating a class for a calendar. That function is part of the class. I have another function that creates the month and the day. I still have to add error handling and validation. I see you added some validation to your function.

    Hey, I already signed up for Snipplr, barely getting familiar with it. I like it.

    Have you ever tried creating a web application like the ones 37signals have? or like box.net?
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

Page 1 of 2 12 LastLast

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
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012