Jump to content

[SOLVED] PHP Date - Last Month - Next Month


Canman2005

Recommended Posts

Hi all

 

I am doing some work with dates and I need some help.

 

I basically have the code

 

<?php
$date = 'month=$_GET['month'].'&year='.$_GET['year'];
print $date;
?>

 

with a url of

 

page.php?month=02&year=2003

 

it produces something like "month=02&year=2008"

 

What I want to do is also be able to print last months date and also next months date, so with the above URL it would print

 

month=01&year=2008

month=02&year=2008

month=03&year=2008

 

Does that make sense?

 

Can anyone help?

 

Thanks in advance

 

Dave

Link to comment
https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/
Share on other sites

You could do something like this:

 

<?php
$time[0] = mktime(0, 0, 0, $_GET['month'], 1, $_GET['year']);
$time[1] = strtotime('-1 month', $time[0]);
$time[2] = strtotime('+1 month', $time[0]);

$date[0] = 'month='.$_GET['month'].'&year='.$_GET['year'];
$date[1] = 'month='.date('d', $time[1]).'&year='.date('Y', $time[1]);
$date[2] = 'month='.date('d', $time[2]).'&year='.date('Y', $time[2]);

echo $date[0];
echo '<br>';
echo $date[1];
echo '<br>';
echo $date[2];
?>

 

<?php
$m = mktime(0, 0, 0, $_GET['month'], 1, $_GET['year']);
$nextm = mktime(0, 0, 0, $_GET['month']+1, 1, $_GET['year']);
$lastm = mktime(0, 0, 0, $_GET['month']-1, 1, $_GET['year']);

 

I used strtotime because if you were to use +1 or -1 It would give 13 and 0 if you used january (1) or december (12)... I don't think mktime will rollover to the next or previous year if you put in 0 or 13...

<?php

$m = mktime(0, 0, 0, $_GET['month'], 1, $_GET['year']);
$nextm = mktime(0, 0, 0, $_GET['month']+1, 1, $_GET['year']);
$lastm = mktime(0, 0, 0, $_GET['month']-1, 1, $_GET['year']);

echo 'month=' . date('m',$lastm) . '&year=' . date('y', $lastm) . '<br>';
echo 'month=' . date('m',$m) . '&year=' . date('y', $m) . '<br>';
echo 'month=' . date('m',$nextm) . '&year=' . date('y', $nextm) . '<br>';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.