开发者

adding dates in php

开发者 https://www.devze.com 2023-03-28 02:02 出处:网络
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 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


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


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'));


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'));
0

精彩评论

暂无评论...
验证码 换一张
取 消