用php获取下一个15日和/或第30天的日期? [英] Get the date of the next 15th and/or 30th day with php?
问题描述
我需要获得今后几个月的即将到来的 15日和 30日的日期。 (如果二月份在范围内,则必须是28/29)。
I require to get the dates of the upcoming 15th and 30ths of the next months as for the current date. (If February is within range it must be 28/29th of course).
我可以使用mktime / strtotime还是使用其他方法?
Can I do this using mktime/strtotime or maybe using another method?
我有这个,但当然这只是得到这个和下个月的最后一天。我需要即将到来的15和30日。
I got this but of course this is for only get the last day of this and next month. I need the upcoming 15ths and 30ths instead.
$cuota1 = date('t-m-Y', strtotime('+15 days'));
return $cuota1;
$cuota2 = date('t-m-Y', strtotime('+30 days'));
return $cuota2;
$cuota3 = date('t-m-Y', strtotime('+45 days'));
return $cuota3;
提前感谢
推荐答案
希望我有你的想法,可能这个解决方案是相当长的,但看起来像是防弹:
Hope I've got your idea right, and may be this solution is quite long, but looks like it's bullet-proof:
$numOfDays = date('t', $todayStamp);
$base = strtotime('+'.$numOfDays.' days', strtotime(date('Y-m-01', $todayStamp)));
$day15 = date('Y-m-15', $base);
$day30 = date('Y-m-'.min(date('t', $base), 30), $base);
其中 $ todayStamp
通常是 time()
,但为了调试的目的,它可以是任意日期的 strtotime()
。例如,让我们来看下一个闰年的困难情况:
where $todayStamp
is generally the value of time()
, but for debug purposes it can be strtotime()
of an arbitrary date. For example, let's take "difficult" case of the next leap year:
$today = '2016-01-30';
$todayStamp = strtotime($today);
$numOfDays = date('t', $todayStamp);
$base = strtotime('+'.$numOfDays.' days', strtotime(date('Y-m-01', $todayStamp)));
$day15 = date('Y-m-15', $base);
$day30 = date('Y-m-'.min(date('t', $base), 30), $base);
var_dump($day15);
var_dump($day30);
输出是
string(10) "2016-02-15"
string(10) "2016-02-29"
而对于20162-29,输出将为
and for 2016-02-29 the output will be
string(10) "2016-03-15"
string(10) "2016-03-30"
这篇关于用php获取下一个15日和/或第30天的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!