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).
Can I do this using mktime/strtotime or maybe using another method?
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;
Thanks in advance.
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);
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);
The output is
string(10) "2016-02-15"
string(10) "2016-02-29"
and for 2016-02-29 the output will be
string(10) "2016-03-15"
string(10) "2016-03-30"
Perhaps a little crude but you could try something like this, though of course this doesn't validate the date at all:-
$m=date('m')+1;
$y=date('Y');
$d=15;
echo date( 't-m-Y', strtotime( "{$d}.{$m}.{$y}" ) );
# > outputs: 31-12-2015
$d=30;
echo date( 't-m-Y', strtotime( "{$d}.{$m}.{$y}" ) );
# > outputs: 31-12-2015
$tstamp = strtotime('+1 month'); //add a month
$m=date('m',$tstamp); //month value
$y=date('Y'); //this year
$date15 = date("Y-m-d", strtotime("$y-$m-"."15")); //15 th of next month
$date31 = date("Y-m-t", strtotime("$y-$m-"."15")); //last date of next month
This will give you last date of next month.