Canman2005 Posted January 22, 2008 Share Posted January 22, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/ Share on other sites More sharing options...
Nhoj Posted January 22, 2008 Share Posted January 22, 2008 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]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445701 Share on other sites More sharing options...
Barand Posted January 22, 2008 Share Posted January 22, 2008 <?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']); Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445707 Share on other sites More sharing options...
Canman2005 Posted January 22, 2008 Author Share Posted January 22, 2008 hummmm cant seem to get either of them to produce the correct result no errors, just either nothing showing or in a format that I cannot seem to decode any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445720 Share on other sites More sharing options...
Nhoj Posted January 22, 2008 Share Posted January 22, 2008 <?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... Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445726 Share on other sites More sharing options...
Barand Posted January 22, 2008 Share Posted January 22, 2008 <?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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445728 Share on other sites More sharing options...
Barand Posted January 22, 2008 Share Posted January 22, 2008 @Nhoj <?php $month = 1; $lastm = mktime(0, 0, 0, $month-1, 1, 2008); echo date('Y-m-d', $lastm); // 2007-12-01 ?> Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445731 Share on other sites More sharing options...
Nhoj Posted January 22, 2008 Share Posted January 22, 2008 Nice.... Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445740 Share on other sites More sharing options...
Canman2005 Posted January 22, 2008 Author Share Posted January 22, 2008 Thanks so much everyone Cheers Quote Link to comment https://forums.phpfreaks.com/topic/87142-solved-php-date-last-month-next-month/#findComment-445742 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.