Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1069
  • Last Modified:

Calendar Date Next Previous Month

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
rgranlund
Asked:
rgranlund
  • 4
  • 3
  • 2
1 Solution
 
mankowitzCommented:
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
 
rgranlundAuthor Commented:
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
 
mankowitzCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
rgranlundAuthor Commented:
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
 
Ray PaseurCommented:
<?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
 
rgranlundAuthor Commented:
@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
 
Ray PaseurCommented:
What do you mean by "to week view?"  Can you show me an example or a mockup?
0
 
rgranlundAuthor Commented:
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
 
Ray PaseurCommented:
I suppose you could do that.  Maybe start with "Last Sunday" if the date('w') > 0?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now