Solved

Calendar Date Next Previous Month

Posted on 2013-10-24
9
765 Views
Last Modified: 2013-12-02
If I am using a calendar,much like practical application #6 form this resource:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

How do I add a Next Month and Previous Month button?
0
Comment
Question by:rgranlund
  • 4
  • 3
  • 2
9 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 39598061
In the example, the default value is today

function little_calendar_table($date='Today')

Open in new window


If you want to have next month, just put in +1 month, as in

little_calendar_table("+1 month")

Open in new window

0
 
LVL 7

Author Comment

by:rgranlund
ID: 39598153
I want the calendar as it is but I want to add to the display  - View Next Moth / View previous Month.

Change month navigation.
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 39598332
you will have to negotiate how your program will handle those links. you may end up with something similar to this

<?php
$st = date("Y-m-01", strtotime($_REQUEST['startime'] ?: "today"));
$next = date("Y-m-d", strtotime($st . "+ 1 month"));
$prev = date("Y-m-d", strtotime($st . "- 1 month"));
little_calendar_table($st);
?>

blah blah

<a href=calendar.php?startimee=<?=$next?>>Next</a>
<a href=calendar.php?startimee=<?=$prev?>>Prev</a>
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39598685
The Following is the complete code that I have developed.  Your solution does not quit work.

	function Calendar($date='Today')	{  //  METHOD

				
			
	
    $timestamp         = strtotime(date('Y-m-01', strtotime($date)));
    $caption           = date("F Y", $timestamp);
    $first_day_number  = date("w", $timestamp);
    $last_day_of_month = date("t", $timestamp);
    $day_counter       = 0;
	
	$todaystamp         = date('Y-m-01');
	 

	//$st = date("Y-m-01", strtotime($_REQUEST['startime'] ?: "Today"));
	//$next = date("Y-m-d", strtotime($st . "+ 1 month"));
	//$prev = date("Y-m-d", strtotime($st . "- 1 month"));
	//little_calendar_table($st);
?>
<table id="cal_nav">
	<tr>
		<td class="cal-nav-left"><h2><< Prev</h2></td>
		<td class="cal-nav-center"><h2><?php echo $caption; ?></h2></td>
		<td class="cal-nav-right"><h2>Next >></h2></td>
	</tr>
</table>


<?php
    echo '<table id="calendar">';
    echo PHP_EOL;

    echo '<tr>';
    echo '<th abbr="Sunday"    width="14%" >S</th>';
    echo '<th abbr="Monday"    width="14%" >M</th>';
    echo '<th abbr="Tuesday"   width="14%" >T</th>';
    echo '<th abbr="Wednesday" width="14%" >W</th>';
    echo '<th abbr="Thursday"  width="14%" >T</th>';
    echo '<th abbr="Friday"    width="14%" >F</th>';
    echo '<th abbr="Saturday"  width="14%" >S</th>';
    echo '</tr>';
    echo PHP_EOL;


    // THE FIRST ROW MAY HAVE DAYS THAT ARE NOT PART OF THIS MONTH
    echo '<tr>';
    while ($day_counter < $first_day_number)
    {
        echo '<td>&nbsp;</td>';
        $day_counter++;
		$todaystamp++;
    }

    // THE DAYS OF THE MONTH
    $mday = 1;
    while ($mday <= $last_day_of_month)
    {

        // THE DAYS OF THE WEEK
        while ($day_counter < 7)
        {
        	
			echo '<td><div class="date-cont"><strong>'.$mday.'<strong></div>';
					
/////////////////////////////////////////////////////////////////////					
					
		$pdo = new PDO("mysql:host=localhost;.....");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$t_stamp = strtotime(date('Y-m', strtotime($date)));
$year_month = date("Y-m", $t_stamp);

$sql = "SELECT first_name, last_name, asset_name, asset_type, DATE_FORMAT(start_date, '%d') AS day, workorder_id
		FROM schedule
		WHERE DATE_FORMAT(start_date, '%Y-%m') = '$year_month'
		GROUP BY schedule_id DESC";
	// CURSOR_SCROLL ALLOWS REPOSITIONING THE CURSOR - LIKE DATA_SEEK() - BUT SADLY NOT WITH MYSQL
$pdos = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

	//  START FIRST QUERY TRY GRAB EMPLOYEE ID FROM SCHEDULE			
	try {
    	$pdos->execute();

// Loop through the results and put events in array
	if ($pdos) {

	//loop through the results
		while ($row = $pdos->fetch(PDO::FETCH_OBJ)) {
			$day = $row->day;
			$fn = $row->first_name;
			$ln = $row->last_name;
			$as = $row->asset_name;	
			$at = $row->asset_type;
			$wid = $row->workorder_id;			
	
			$today = (int)$day;			
										
			if ($mday == $today) {
				echo '<a href="view_workorders.php">View Job</a><br />';	
			}
		}  //  END WHILE
	}  //  END IF
	}  //  END TRY			          	
	catch(PDOException $e) {
   		echo 'ERROR: ' . $e->getMessage();
	}			
				
/////////////////////////////////////////////////////////////////////				
				
				
				

			echo '</td>';
            $day_counter++;
            $mday++;
            if ($mday > $last_day_of_month) break;
        }

        echo '</tr>';
        echo '<tr>';
        $day_counter = 0;
    }

    // THE LAST ROW MAY HAVE DAYS THAT ARE NOT PART OF THIS MONTH
    while ($day_counter < 7)
    {
        echo '<td>&nbsp;</td>';
        $day_counter++;
    }

    echo '</tr>';
    echo '</table>';
    echo PHP_EOL;
		
	}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39599029
<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28276327.html
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

function little_calendar_table($date='Today')
{
    $timestamp         = strtotime(date('Y-m-01', strtotime($date)));
    $caption           = date("F Y", $timestamp);
    $first_day_number  = date("w", $timestamp);
    $last_day_of_month = date("t", $timestamp);
    $day_counter       = 0;
    $html              = NULL;

    $html .= '<table>' . PHP_EOL;
    $html .= '<caption style="text-align:left;">' . $caption . '</caption>';
    $html .= PHP_EOL;

    $html .= '<tr>';
    $html .= '<th abbr="Sunday"    width="14%" >S</th>';
    $html .= '<th abbr="Monday"    width="14%" >M</th>';
    $html .= '<th abbr="Tuesday"   width="14%" >T</th>';
    $html .= '<th abbr="Wednesday" width="14%" >W</th>';
    $html .= '<th abbr="Thursday"  width="14%" >T</th>';
    $html .= '<th abbr="Friday"    width="14%" >F</th>';
    $html .= '<th abbr="Saturday"  width="14%" >S</th>';
    $html .= '</tr>';
    $html .= PHP_EOL;


    // THE FIRST ROW MAY HAVE DAYS THAT ARE NOT PART OF THIS MONTH
    $html .= '<tr>';
    while ($day_counter < $first_day_number)
    {
        $html .= '<td>&nbsp;</td>';
        $day_counter++;
    }

    // THE DAYS OF THE MONTH
    $mday = 1;
    while ($mday <= $last_day_of_month)
    {
        // THE DAYS OF THE WEEK
        while ($day_counter < 7)
        {
            $html .= '<td style="text-align:right;">' . " $mday</td>";
            $day_counter++;
            $mday++;
            if ($mday > $last_day_of_month) break;
        }

        $html .= '</tr>' . PHP_EOL;
        $html .= '<tr>';
        $day_counter = 0;
    }

    // THE LAST ROW MAY HAVE DAYS THAT ARE NOT PART OF THIS MONTH
    while ($day_counter < 7)
    {
        $html .= '<td>&nbsp;</td>';
        $day_counter++;
    }

    $html .= '</tr>' . PHP_EOL;
    $html .= '</table>' . PHP_EOL;
    $html .= PHP_EOL;

    return $html;
}

$date = !empty($_GET['d']) ? $_GET['d'] : 'Today';
$prev = date('Y-m-01', strtotime("$date - 1 month"));
$next = date('Y-m-01', strtotime("$date + 1 month"));
$prev_link = '<a href="' . $_SERVER['PHP_SELF'] . "?d=$prev" . '">Prev</a>' . PHP_EOL;
$next_link = '<a href="' . $_SERVER['PHP_SELF'] . "?d=$next" . '">Next</a>' . PHP_EOL;
$calt = little_calendar_table($date);
$html = <<<EOD
<table>
<tr valign="top">
<td>$prev_link</td>
<td>$calt</td>
<td>$next_link</td>
</tr>
</table>
EOD;
echo $html;

Open in new window

HTH, ~Ray
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39612560
@ray, thank you for this.  Is there one of the examples that is on your document that shows How I can change the above script to week view?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39612569
What do you mean by "to week view?"  Can you show me an example or a mockup?
0
 
LVL 7

Author Comment

by:rgranlund
ID: 39614377
What I mean is this;
Right now you view an entire month.  I was wondering if you view the calendar week by week? One week at a time?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39614399
I suppose you could do that.  Maybe start with "Last Sunday" if the date('w') > 0?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now