Date and mktime

I didn’t use date or mktime at all before, and now I follow a tutorial for those to learn more.
For this code:

$ts = strtotime($this->_useDate);
    $this->_m = date('m', $ts);
    $this->_y = date('y', $ts);
    
    /*
    * Determine how many days are in the month
    */
    $this->_daysInMonth = cal_days_in_month(
                            CAL_GREGORIAN,
                            $this->_m, 
                            $this->_y);
    
    /*
    * Determine what weekday the month starts on
    */
    $ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
    $this->_startDay = date('w', $ts);

I get this:

object(Calendar)[2]

private ‘_useDate’ => string ‘2016-01-01 12:00:00’ (length=19)
private ‘_m’ => string ‘01’ (length=2)
private ‘_y’ => string ‘16’ (length=2)
private ‘_startDay’ => string ‘5’ (length=1)
protected ‘db’ =>
object(PDO)[1]
public ‘_daysInMonth’ => int 31

I know that string ‘5’ comes from the date(‘w’, $ts), but I don’t understand why 5 and not 1 or 2?

1st Jan 2016 is a Friday?

You are right droopsnoot :). Thank you. My mind was focused too much on php code and I forgot about the reality :).

On the same tutorial I get some errors about a variable: $event_info. Even this shows me on the page, what brings from database, I get

Notice: Undefined variable: event_info in C:\wamp\www

Can someone please tell me, what is wrong? Because I don’t see any mistake. The problem it is in buildCalendar().

public function buildCalendar()
    {
        /*
         * Determine the calendar month and create an array of
         * weekday abbreviations to label the calendar columns
         */
        $cal_month = date('F Y', strtotime($this->_useDate));
        $weekdays = array('Sun', 'Mon', 'Tue',
                'Wed', 'Thu', 'Fri', 'Sat');

        /*
         * Add a header to the calendar markup
         */
        $html = "\n\t<h2>$cal_month</h2>";
        for ( $d=0, $labels=NULL; $d<7; ++$d )
        {
            $labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
        }
        $html .= "\n\t<ul class=\"weekdays\">"
            . $labels . "\n\t</ul>";

        /*
         * Load events data
         */
        $events = $this->_createEventObj();

        /*
         * Create the calendar markup
         */
        $html .= "\n\t<ul>"; // Start a new unordered list
        for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
                $c<=$this->_daysInMonth; ++$i )
        {
            /*
             * Apply a "fill" class to the boxes occurring before
             * the first of the month
             */
            $class = $i<=$this->_startDay ? "fill" : NULL;

            /*
             * Add a "today" class if the current date matches
             * the current date
             */
            if ( $c==$t && $m==$this->_m && $y==$this->_y )
            {
                $class = "today";
            }

            /*
             * Build the opening and closing list item tags
             */
            $ls = sprintf("\n\t\t<li class=\"%s\">", $class);
            $le = "\n\t\t</li>";

            /*
             * Add the day of the month to identify the calendar box
             */
            if ( $this->_startDay<$i && $this->_daysInMonth>=$c)
            {
                /*
                 * Format events data
                 */
                $event_info = NULL; // clear the variable
                if ( isset($events[$c]) )
                {
                    foreach ( $events[$c] as $event )
                    {
                        $link = '<a href="view.php?event_id='
                                . $event->id . '">' . $event->title
                                . '</a>';
                        $event_info .= "\n\t\t\t$link";
                    }
                }

                $date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);
            }
            else { $date="&nbsp;"; }

            /*
             * If the current day is a Saturday, wrap to the next row
             */
            $wrap = $i!=0 && $i%7==0 ? "\n\t</ul>\n\t<ul>" : NULL;

            /*
             * Assemble the pieces into a finished item
             */
            
            $html .= $ls . $date . $event_info . $le . $wrap;
            
        }

        /*
         * Add filler to finish out the last week
         */
        while ( $i%7!=1 )
        {
            $html .= "\n\t\t<li class=\"fill\">&nbsp;</li>";
            ++$i;
        }

        /*
         * Close the final unordered list
         */
        $html .= "\n\t</ul>\n\n";

        /*
         * Return the markup for output
         */
        return $html;
    }

You define $event_info inside the first if-condition in your code, so if the if-condition is not met, the variable is not created. But you reference it outside the if-condition whether it was created or not. Move the line that says $event_info=NULL to before the if-condition is opened, that way it will be defined regardless.

Who knows, knows :). Thank you dropsnoot.
Because is not my tutorial, this one comes with Sunday the first day of the week. I tried to change this array from

$weekdays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

to

$weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');

but I didn’t understand what else to change the date number, too. After this array modified the 1st january 2016 is saturday and not anymore friday. If you have any idea feel free to tell me, because I tried without any luck.

I’m not exactly sure what you’re trying to do by changing the array - the day-code for Sunday from mktime() and date() is zero, so just changing the label array will do as you say - put the wrong day names in each date.

I want the week to start from monday, not from sunday, how it is right now in the code. That’s why I want to change in the array the first day to be Mon and not Sun.

I can’t quite picture what the output is going to be like, but I’d suspect the code you need to look at is here:

             * If the current day is a Saturday, wrap to the next row
             */
            $wrap = $i!=0 && $i%7==0 ? "\n\t</ul>\n\t<ul>" : NULL;

That checks the modulo of $i against 7 having a value zero. I think if you change it to this, it might help, but I’m not completely sure:

             * If the current day is a Saturday, wrap to the next row
             */
            $wrap = $i!=0 && $i%7==1 ? "\n\t</ul>\n\t<ul>" : NULL;

See, I’ve changed the check to look for the modulo being one instead of zero. You might have to change the loop at the top that draws the weekdays, or maybe re-arrange the array as you said above. I suspect I’ve missed something but I can’t test it.

I don’t understand what the author did in in that for. Thank you very much anyway. I will drop this tutorial.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.