1

I have the following statement. Either i get the date from the querystring or i get todays date.

I then need to get the current, previous, next month.

I think i'm going wrong by using "strtotime"

$selecteddate = ($_GET ['s'] == "" )
    ? getdate()
    : strtotime ($_GET ['s']) ;


    $previousMonth = strtotime(date("Y-m-d", $selecteddate) . " +1 month");

    $previousMonthName = $previousMonth[month];
    print $previousMonthName;
    $month = $selecteddate[month];

/* edit */

$selecteddate = ($_GET ['s'] == "" )
? getdate()
: strtotime ($_GET ['s']) ;

$previousMonth = strtotime(" -1 month", $selecteddate);
$nextMonth = strtotime(" +1 month", $selecteddate);


$previousMonthName = date("F",$previousMonth); //Jan
$nextMonthName = date("F",$nextMonth); // Jan
$month = $selecteddate[month];  // Aug
1
  • re. your edit; the $selecteddate will either hold an array (returned from getdate()) or an integer (returned from strtotime()). The later calls to strtotime() will not be happy at all if passed the array. Commented Aug 15, 2011 at 21:10

3 Answers 3

2

you're almost right - just replace

$previousMonth = strtotime(date("Y-m-d", $selecteddate) . " +1 month");

by

$previousMonth = strtotime(" +1 month", $selecteddate);

take a look at the documentation to learn more about the second parameter (called "$now"). to get the month names, do this (documentation again):

$previousMonthName = date("F",$previousMont);
$month = date("F",$selecteddate); // not sure if you want to get the monthname here,
                                  // but you can use date() to get a lot of other
                                  // values, too
Sign up to request clarification or add additional context in comments.

3 Comments

wait i spoke to soon, see above. Is it returning Jan because it is not passing correctly?
and if you are using 5.3 you might want to check out dateInterval and then you can use the format $nextMonth->add(new DateInterval('P1M'));
Using +1 month (or its P1M friend) for this task is asking for trouble when adjacent months are shorter than the current/target month.
1

oezi's answer will run into problems toward the end of some months. This is due to PHP's interpretation of ±1 month which simply increments/decrements the month, then adjusts the day part as appropriate.

For example, given 31 October and +1 month the date will become 31 November which does not exist. PHP takes that into account and roles the date around to 1 December. The same would happen for -1 month to become 1 October.

Various alternative approaches exist, one of which is to set modify the date explicitly with (the little used) DateTime::setDate() as appropriate.

// e.g. $selecteddate = time();

$now = new DateTime;
$now->setTimestamp($selecteddate);

// Clone now to hold previous/next months
$prev = clone $now;
$next = clone $now;

// Alter objects to point to previous/next month
$prev->setDate($now->format('Y'), $now->format('m') - 1, $now->format('d'));
$next->setDate($now->format('Y'), $now->format('m') + 1, $now->format('d'));

// Go wild
var_dump($prev->format('r'), $next->format('r'));

Comments

1

I think salathe's answer might actually fall over the same problem he pointed out in oezi's answer. He passed $now->format('d') to setDate() as the day number but in a month with 31 days that might not make sense if the target month only has 30 days. I'm not sure what SetDate will do if you try to set a date that isn't sane - most likely throw an error. But the solution is very simple. All months have a day number 1. Here's my version of salathe's code.

// e.g. $selecteddate = time();

$now = new DateTime;
$now->setTimestamp($selecteddate);

// Clone now to hold previous/next months
$prev = clone $now;
$next = clone $now;

// Alter objects to point to previous/next month. 
// Use day number 1 because all the questioner wanted was the month.
$prev->setDate($now->format('Y'), $now->format('m') - 1, 1);
$next->setDate($now->format('Y'), $now->format('m') + 1, 1);

// Go wild
var_dump($prev->format('r'), $next->format('r'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.