好的,我想我找到了解决方案,它的行为类似于 MySQLDATE_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