6 Replies - 1484 Views - Last Post: 01 October 2012 - 06:44 PM Rate Topic: -----

#1 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Find the weekend dates in Calendar

Posted 01 October 2012 - 04:46 PM

I have a calender that I have been fooling around with and I've hit a wall. I'm trying to color the weekends of my calendar a different color than the rest of the dates and I'm having a hard time figuring out how to find the dates with my logic. I'm fairly new to PHP but I'm hooked. I'm learning more everyday and this place keeps me engaged.

Anyway, here is my code...

<?php

class Ajax_Calendar
{
    /*
     * Define custom callback functions (optional)
     */
    # using a callback function like this is optional
    public function day_details($y, $m, $d)
    {
        # blank cells before day 1 or after day 30/31
        if ($day == '') {
            echo '&nbsp;'; # for IE table cells
        }
        echo "$d";
        if ($m == 1 and $d == 1) {
            echo "<br/><em>New Year's Day!</em>";
        }
    }
    
    function link_to_events($year, $month, $day, $date_data)
    {
        # blank cells before day 1 or after day 30/31
        if ($day == '')
            echo '&nbsp;'; # for IE table cells
        
        # Your date_array should tell us how many events are on the given day...
        if ('0' < $date_data) {
            echo '<a href="/calendar/list_events/' . "$year/$month/$day" . '" class="day_link">' . $day . '</a>';
        } else
            echo '<div class="day_simple">' . $day . '</div>';
    }
    
    public function getPhpAjaxCalendarCore($month, $year, $date_array = NULL, $day_function = null)
    {
        $current_time = time();
        $month_start  = mktime(0, 0, 0, $month, 1, $year);
        $month_name   = date('F', $month_start);
        $first_day    = date('D', $month_start);
        switch ($first_day) {
            case "Sun":
                $offset = 0;
                break;
            case "Mon":
                $offset = 1;
                break;
            case "Tue":
                $offset = 2;
                break;
            case "Wed":
                $offset = 3;
                break;
            case "Thu":
                $offset = 4;
                break;
            case "Fri":
                $offset = 5;
                break;
            case "Sat":
                $offset = 6;
                break;
        }
        if ($month == 1)
            $num_days_last = cal_days_in_month(CAL_GREGORIAN, 12, ($year - 1));
        else
            $num_days_last = cal_days_in_month(CAL_GREGORIAN, ($month - 1), $year);
        $num_days_current = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        for ($i = 0; $i < $num_days_current; $i++) {
            $num_days_array[] = $i + 1;
        }
        for ($i = 0; $i < $num_days_last; $i++) {
            $num_days_last_array[] = '';
        }
        if ($offset > 0) {
            $offset_correction = array_slice($num_days_last_array, -$offset, $offset);
            $new_count         = array_merge($offset_correction, $num_days_array);
            $offset_count      = count($offset_correction);
        } else {
            $new_count = $num_days_array;
        }
        $current_num = count($new_count);
        
        if ($current_num > 35) {
            $num_weeks = 6;
            $outset    = (42 - $current_num);
        } else if ($current_num < 35) {
            $num_weeks = 5;
            $outset    = (35 - $current_num);
        }
        if ($current_num == 35) {
            $num_weeks = 5;
            $outset    = 0;
        }
        
        // Outset Correction
        for ($i = 1; $i <= $outset; $i++) {
            $new_count[] = '';
        }
        $weeks = array_chunk($new_count, 7);
        
        // Start the output buffer so we can output our calendar nicely
        ob_start();
        
        $prev_month = $month - 1;
        $prev_year  = $year;
        if ($month == 1) {
            $prev_month = 12;
            $prev_year  = $year - 1;
        }
        
        $next_month = $month + 1;
        $next_year  = $year;
        if ($month == 12) {
            $next_month = 1;
            $next_year  = $year + 1;
        }
        
        // Build the heading portion of the calendar table
        echo <<<EOS
			<table class="calendar fluid large-margin-bottom">
		
				<caption>
					<a href="?month=$prev_month&year=$prev_year" class="next" id="arrow_left">◄</a>
					<a href="?month=$next_month&year=$next_year" class="next" id="arrow_right">►</a>
					$month_name $year
				</caption>
				<thead>
					<tr>
						<th scope="col">Sun</th>
						<th scope="col">Mon</th>
						<th scope="col">Tue</th>
						<th scope="col">Wed</th>
						<th scope="col">Thu</th>
						<th scope="col">Fri</th>
						<th scope="col">Sat</th>
					</tr>
				</thead><tbody>
EOS;
        
        foreach ($weeks AS $week) {
            echo '<tr class="week">';
            foreach ($week as $day) {
                if ($day == date('d', $current_time) && $month == date('m', $current_time) && $year == date('Y', $current_time))
                    echo '<td class="today"><span class="cal-day">';
                else
                    echo '<td><span class="cal-day">';
                if (null == $day_function OR empty($date_array[$day])) {
                    echo $day;
                } else {
                    /* Calls user function from this class,
                     * Sends appropriate date data from date_array
                     */
                    call_user_func(array(
                        $this,
                        $day_function
                    ), $year, $month, $day, $date_array[$day]);
                }
                echo '</td>';
            }
            echo '</tr>';
        }   
        echo '</tbody></table>';
        
        return ob_get_clean();
    }
    
}

?>


Every thing works great except the weekend thing, and the fact that I also need to figure out how to set the calendar for my time zone. I'm at UTC/GMT -4 hours.

Anyway, thanks for everyone's help.

This post has been edited by Java Neil: 01 October 2012 - 04:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Find the weekend dates in Calendar

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,753
  • Joined: 08-August 08

Re: Find the weekend dates in Calendar

Posted 01 October 2012 - 05:27 PM

Date() can return the day number, 0 - 6, starting with Sunday.
Was This Post Helpful? 1
  • +
  • -

#3 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: Find the weekend dates in Calendar

Posted 01 October 2012 - 06:06 PM

View PostCTphpnwb, on 01 October 2012 - 07:27 PM, said:

Date() can return the day number, 0 - 6, starting with Sunday.


Yes, I've been reading about this function, but I'm not sure how to implement it!
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,753
  • Joined: 08-August 08

Re: Find the weekend dates in Calendar

Posted 01 October 2012 - 06:20 PM

Example:
<?php
$year = 2012;
$month = 10;
$day_name = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
// Note that you can also get day names with date(). This is just to demonstrate that the numbers are correct.

for($day = 1; $day < 32; $day++) {
	$date_time = strtotime($year.'/'.$month.'/'.$day);
	$day_of_week = date("w",$date_time);
	echo $day_of_week." = ".$day_name[$day_of_week]."<br>";
}

Was This Post Helpful? 2
  • +
  • -

#5 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: Find the weekend dates in Calendar

Posted 01 October 2012 - 06:36 PM

Well I thought I had this logic for a second but I was sadly mistaken.

for($day = 1; $day < 32; $day++) {
	$date_time = strtotime($year.'/'.$month.'/'.$day);
	$day_of_week = date("w",$date_time);
	if ($day_of_week == 0 || 6)
	echo $day_of_week." = ".$day_name[$day_of_week]."<br>";
}


This still printed out all the dates? Why did this not work.

I have soooo much to learn!

This post has been edited by Java Neil: 01 October 2012 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3069
  • View blog
  • Posts: 10,753
  • Joined: 08-August 08

Re: Find the weekend dates in Calendar

Posted 01 October 2012 - 06:40 PM

Because 6 is true.
Try:
    if ($day_of_week == 0 || $day_of_week == 6)


Was This Post Helpful? 2
  • +
  • -

#7 Java Neil  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 140
  • Joined: 26-March 11

Re: Find the weekend dates in Calendar

Posted 01 October 2012 - 06:44 PM

View PostCTphpnwb, on 01 October 2012 - 08:40 PM, said:

Because 6 is true.
Try:
    if ($day_of_week == 0 || $day_of_week == 6)



See...I just learned something. Kudos to you. I mean it, you deserve it,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1