Jump to content

Recommended Posts

I have the following code:

 

//get the current month from the url
$month = $_GET['mon'];

//make the month into words
$real_month = getdate(mktime(0,0,0,$month,$date['mday'],$date['year']));

//get the current month
$date = getdate();

//get the previous month
$date_previous_month = getdate(mktime(0,0,0,$month-1,$date['mday'],$date['year']));
$previous_month = $date_previous_month['month'];

//get the next month
$date_next_month = getdate(mktime(0,0,0,$month+1,$date['mday'],$date['year']));
$next_month = $date_next_month['month'];
?>

<div id="calendar">
<center>
<table>
<th colspan="7">
<a href="calendar.php?mon=<?php echo $date_previous_month['mon']; ?>"><<</a> 
<?php echo $real_month['month']." ".$date['year'];; ?> 
<a href="calendar.php?mon=<?php echo $date_next_month['mon']; ?>">>></a>
</th>
</table>
</center>

</div>

 

There is more to it but that is all that is in use. Now in the url it passes the current month in a number format when you reach this page. At the current time It is showing the correct month and year and goes back and forth. The only problem is that when you go past december it does not change to the next year or when you go to before january it doesnt go back a year. I am just trying to create a simple calendar just for testing purposes. If anyone can help it would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/128758-solved-date/
Share on other sites

You're better off passing both the year and the month in the query string... you can then do something like this

 

if( $_GET['m'] == 1 ) {
$prevMonth = 12;
$prevYear = $_GET['y'] - 1;
} elseif( $_GET['m'] == 12 ) {
$nextMonth = 1;
$nextYear = $_GET['y'] + 1;
}

Link to comment
https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667391
Share on other sites

try

<?php                                  
$month = isset($_GET['m']) ? $_GET['m'] : date('n');
$year =  isset($_GET['y']) ? $_GET['y'] : date('Y');

$prevm = $month==1 ? 12 : $month-1;
$prevy = $month==1 ? $year-1 : $year;

$nextm = $month==12 ? 1 : $month+1;
$nexty = $month==12 ? $year+1 : $year;

echo "<a href='?m=$prevm&y=$prevy'><<</a> ";
echo date('M Y', mktime(0,0,0,$month,1,$year));
echo " <a href='?m=$nextm&y=$nexty'>>></a>";

?>

Link to comment
https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667572
Share on other sites

Instead of fooling with manual date calculations, let PHP do them for you:

<?php
//get the current month from the url
$month = isset($_GET['mon'])?$_GET['mon']:date('m');
$year = isset($_GET['yr'])?$_GET['yr']:date('Y');
$passed_my = strtotime($year . '-' . $month . '-01');

//get the previous month
$date_previous_month = date('m',strtotime('-1 month',$passed_my));
$date_next_month = date('m',strtotime('+1 month',$passed_my));
$date_previous_year = date('Y',strtotime('-1 month',$passed_my));
$date_next_year = date('Y',strtotime('+1 month',$passed_my));
?>
<table>
<th colspan="7">
<a href="?mon=<?php echo $date_previous_month; ?>&yr=<?php echo $date_previous_year; ?>"><<</a> 
<?php echo date('F Y',$passed_my); ?> 
<a href="?mon=<?php echo $date_next_month; ?>&yr=<?php echo $date_next_year; ?>">>></a>
</th>
</table>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/128758-solved-date/#findComment-667614
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.