Solved

Calendar Date Next Previous Month

Posted on 2013-10-24
9
742 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
<?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
Comment Utility
@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
Comment Utility
What do you mean by "to week view?"  Can you show me an example or a mockup?
0
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
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
Comment Utility
I suppose you could do that.  Maybe start with "Last Sunday" if the date('w') > 0?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 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

11 Experts available now in Live!

Get 1:1 Help Now