|
|
1
9
更新: 这将为您提供给定月份的天数:
我不确定您正在考虑的算法,但我相信这将帮助您:
|
|
|
2
5
|
|
|
3
4
很多时间过去了,但如果有人在搜索这个,在上面的所有答案中,我发现只有@ling答案真正回答了这个问题,但他的答案仍然不是百分之百准确,正如你在上面的结果中看到的那样,它错误地输出了年份部分。 这是他的代码的修改版本,用于修复年份问题(我还将其修改为使用DateTime类)
输出:
|
|
|
4
3
更直接的点(编辑)
|
|
|
5
2
我建议在php.net上阅读以下评论。 strtotime() (php.net) 编辑:下一个答案给出了我发布的链接的“摘要”,这些链接是为那些无法解读那里显示的内容的人提供的。 |
|
|
6
1
或者简单地说,
|
|
|
7
1
因此:
|
|
8
1
那么为什么不单独存储月份和日期,逐月递增,然后使用PHP的
|
|
9
1
因此,与其试图计算一个月的天数(这有点复杂),不如使用当前月份轻松地找出下一个月,例如:
然后使用
|
|
|
10
1
易于理解的
|
|
|
11
1
|
|
|
12
0
这个功能怎么样:
php上还有另一个解决方案。net手动站点下的strottime条目 comments . |
|
|
13
0
我也有同样的问题,但当我尝试上述解决方案时,它们对我来说并不完美,我尝试了自己的方法,并提出了新的解决方案,即:
|
|
|
14
0
Datetime OOP样式
|
|
|
15
0
这是我用的 这是当月日期的时间戳
这是下个月的同一天,格式为“Y-m-d”
|
|
|
16
0
我创建了一个名为my_strotime的函数,可以处理月份的加减。它基本上是对strottime的包装。 它检查加上或减去一个月是否可以安全地在同一天得到所需的月份。如果没有,那么它会安全地完成。 当添加月份时,它会添加15以使您安全地进入下一个月,然后返回该月的最后一天。 注:如前所述,本文件有效期为+1个月或-1个月
function _my_strtotime($offset, $reference_date){
//$offset is a string such as (+1 month)
if(strpos($offset, 'month') === false){
//exit if not addin/subtracing months
return strtotime($offset, $reference_date);
}
$other_months_date = strtotime ( $offset, $reference_date );
$day_of_other_month = date( 'd', $other_months_date );
$day_of_reference_month = date('d', $reference_date );
//check if numerical day is different. If so manipulate.
if($day_of_reference_month != $day_of_other_month){
if(strpos($offset, '-') === false){
// adding months
return strtotime('last day of this month', strtotime('+15 days', $reference_date));
} else {
// subtracing months
return strtotime('-1 day', strtotime('first day of this month', $reference_date));
}
}
return strtotime($offset, $reference_date);
}
测试结果:
input=2016-01-31 (+1 month) output=2016-02-29 //leap year
input=2017-01-31 (+1 month) output=2017-02-28
input=2018-05-31 (+1 month) output=2018-06-30
input=2018-07-31 (-1 month) output=2018-06-30
input=2016-07-22 (-1 month) output=2016-06-22
input=2016-07-22 (+1 month) output=2016-08-22
|
|
|
feasega · 聚合物模拟-2个节点之间的最短路线,适用于所有节点 1 年前 |
|
|
Alisa Petrova · 在有向图中更改一对顶点以创建循环 1 年前 |
|
|
b39b332d · 使用C++标准库实现高效间隔存储 2 年前 |
|
ABGR · 二叉树的直径——当最长路径不通过根时的失败案例 2 年前 |
|
|
EpicAshman · 数独棋盘程序中同一列和同一行出现两次的数字 2 年前 |