gpt4 book ai didi

PHP:与下个月相关的日期函数结果不正确(PHP 错误或 sg.else?)

转载 作者:可可西里 更新时间:2023-11-01 01:06:01 25 4
gpt4 key购买 nike

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

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

  • date('', 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 class : http://pastebin.com/3iaH4iSZ

输出如下所示 ( you can also see it here! ): PHP date functions related to NEXT month

这些是 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

最佳答案

好的,我想我找到了解决方案,它的行为类似于 MySQL 的 DATE_ADD 函数和 INTERNAL 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

关于PHP:与下个月相关的日期函数结果不正确(PHP 错误或 sg.else?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10834350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com