3

我尝试了一些日期函数,因为我想用 PHP 获取下个月的一些日期,但我遇到了一些问题。

作为 MySQL 兼容时间戳 ( date('Y-m-d H:i:s');) 的当前日期如下:
'2012-05-31 14:59:19'

  • date('t', strtotime('next month'));它给出了下个月的天数,结果返回31 ......这是不正确的,因为6 月仅包含 30 天(似乎它输出了 7 月的天数)。
  • date('Y-m-d H:i:s', strtotime('next month'));,它给出了下个月的时间戳格式,返回'2012-07-01 14:59:19',但我希望如下
    '2012-06-30 14:59:19'

所以我尝试了一些代码,这是我的实验,使用date()strtotime()DateTime classhttp: //pastebin.com/3iaH4iSZ

输出如下所示(您也可以在这里看到它!): 下个月相关的PHP日期函数

这些是 PHP 错误,还是我误解了什么?


我希望得到与使用 MySQL 的日期函数时相同的结果,仅举几个例子:

SELECT NOW( )

2012-05-31 17:51:15

SELECT DATE_ADD(NOW(), INTERVAL 1 MONTH)

2012-06-30 17:51:15

SELECT DATE_ADD( '2013-01-30 17:51:15', INTERVAL 1 MONTH )

2013-02-28 17:51:15

SELECT DATE_ADD( '2013-01-31 17:51:15', INTERVAL 1 MONTH )

2013-02-28 17:51:15

4

3 回答 3

2

好的,我想我找到了解决方案,它的行为类似于 MySQLDATE_ADDINTERNAL 1 MONTH.
我用所有可能有问题的情况测试了我的功能,它似乎工作正常。
我把它贴在这里,也许有人会觉得它有用。我在这段代码中添加了一些注释,所以我认为它已经足够清楚了。

该函数被调用getOneMonthLaterTimestamp。一个例子:

echo getOneMonthLaterTimestamp('2012-05-31 17:51:15');

输出:2012-06-30 17:51:15

功能

/**
 * Test function for dumping variables in a readable way
 * 
 * @param mixed $stuff
 * @param string $text
 * @return string 
 */
function my_var_export($stuff, $text = '...') {
    return '<p>' . $text . ' (' . gettype($stuff) . '):</p><pre>' . var_export($stuff, TRUE) . '</pre>';
}

/**
 * Get timestamp format of the date one month later 
 * than the date given in the argument/current date if left empty.
 * 
 * Behaves similar to MySQL's DATE_ADD function with INTERVAL 1 MONTH:
 * SELECT DATE_ADD('2012-05-31 17:51:15', INTERVAL 1 MONTH)
 * --> 2012-06-30 17:51:15
 * SELECT DATE_ADD('2013-01-30 17:51:15', INTERVAL 1 MONTH )
 * --> 2013-02-28 17:51:15
 * 
 * these are equivalent to:
 * echo getOneMonthLaterTimestamp('2012-05-31 17:51:15');
 * echo getOneMonthLaterTimestamp('2013-01-30 17:51:15');
 * 
 * You can also call it without an argument. This way, the current date is taken as a basis.
 * echo getOneMonthLaterTimestamp();
 * 
 * @param string $DateTime_param date/time string
 * @see http://www.php.net/manual/en/datetime.formats.php
 * @see http://www.php.net/manual/en/datetime.construct.php
 * 
 * @return string Date one month later as a MySQL-compatible timestamp format
 */
function getOneMonthLaterTimestamp($DateTime_param = NULL) {
    // if argument is left empty, the current date is taken as a basis
    if (empty($DateTime_param)) {
        $DateTime_param = date('Y-m-d H:i:s');
    }

    $lastDayOfNextMonth = new DateTime($DateTime_param);
    $lastDayOfNextMonth->modify('last day of next month');

    $nextMonth = new DateTime($DateTime_param);
    $nextMonth->modify('next month');

    if ($lastDayOfNextMonth->format('n') < $nextMonth->format('n')) {
        $oneMonthLaterTimestamp = $lastDayOfNextMonth->format('Y-m-d H:i:s');
    }
    else {
        $oneMonthLaterTimestamp = $nextMonth->format('Y-m-d H:i:s');
    }

    return $oneMonthLaterTimestamp;
}

测试用例

$timestamps_to_test_array = array(
  date('Y-m-d H:i:s'),   // 1. current date
  '2011-01-28 23:59:59', // 2.
  '2011-01-29 23:59:59', // 3.
  '2011-01-30 23:59:59', // 4.
  '2011-01-31 23:59:59', // 5.
  '2012-01-28 23:59:59', // 6.
  '2012-01-29 23:59:59', // 7.
  '2012-01-30 23:59:59', // 8.
  '2012-01-31 23:59:59', // 9.
  '2012-02-29 23:59:59', // 10.
  '2012-03-30 23:59:59', // 11.
  '2012-03-31 23:59:59', // 12.
  '2012-04-30 23:59:59', // 13.
  '2012-05-31 23:59:59', // 14.
  '2012-12-31 23:59:59', // 15.
  '2013-01-31 23:59:59', // 16.
  '2013-02-28 23:59:59', // 17.
);

$i = 1;

foreach ($timestamps_to_test_array as $timestamp_to_test) {
    $oneMonthLaterTimestamp = getOneMonthLaterTimestamp($timestamp_to_test);
    echo my_var_export($timestamp_to_test, '[' . $i . ']. Timestamp to test ($timestamp_to_test)');
    echo my_var_export($oneMonthLaterTimestamp, 'Timestamp + 1 month (getOneMonthLaterTimestamp($timestamp_to_test))');
    echo '<hr />';
    $i++;
}

测试用例的输出

我在这里粘贴了输出:http: //pastebin.com/rY5ZRBs9
这是它的截图:http: //i.imgur.com/KwlJq.png

获取下个月的天数

/**
 * Get number of days in the next month
 *
 * @param string $DateTime_param date/time string
 * @return int Number of days in the next month
 */
function getNumberOfDaysInNextMonth($DateTime_param = NULL) {
    // if argument is empty, the current date is taken as a basis
    if (empty($DateTime_param)) {
        $DateTime_param = date('Y-m-d H:i:s');
    }

    // DateTime instance
    $dateCurrent = new DateTime($DateTime_param);
    $dateCurrent->modify('last day of next month');
    return (int)$dateCurrent->format('t');
}

/**
 * It's identical to getNumberOfDaysInNextMonth()
 *
 * @see getNumberOfDaysInNextMonth
 */
function getLastDayOfNextMonth($DateTime_param = NULL) {
    return getNumberOfDaysInNextMonth($DateTime_param);
}

测试用例

$lastDayOfNextMonth = getLastDayOfNextMonth('2012-05-31 03:50:27');
echo my_var_export($lastDayOfNextMonth, "Get last day of next month (getLastDayOfNextMonth('2012-05-31 03:50:27'))");

输出

Get last day of next month (getLastDayOfNextMonth('2012-05-31 03:50:27')) (integer):
30
于 2012-06-01T01:39:29.613 回答
1

至此:

获取下个月作为时间戳 [ date('Ymd H:i:s', strtotime('next month')); ]:
'2012-07-01 14:59:19'

使用 DateTime 类 [ $dateObj->add(new >DateInterval('P1M')); 获取下个月的最后一天作为时间戳 $dateObj->format('Ymd H:i:s'); ]:
'2012-07-01 14:59:19'

很明显,“下个月”输出 7 月的日期......

试试strtotime('+1 month')吧。

编辑:说明: 今天是 5 月 31 日。如果您称它为strtotime('next month')尝试获取不正确的 6 月 31 日的日期,那么它非常正确地输出 7 月 1 日的日期......因此我猜这strtotime('+1 month')也会失败......

所以更好的解决方案是获取当月第 15 天的日期并使用“下个月”或“+1 个月”到该日期......

于 2012-05-31T13:18:55.103 回答
1

您可以通过一些小的操作来实现您想要的(即,如果我的问题是正确的)

$next_month = mktime(0, 0, 0, date("m")+1, 1, date("Y"));
//gets next month
echo "<br />".date("D jS M, Y", $next_month);
//sets the value
echo "<br />".date("t", $next_month);
//number of days next month

这应该可以让您访问您想要的月份,因为每个月都有first一天。换句话说,要获得下一年,您只需更改适当的值

于 2012-05-31T13:31:39.283 回答