I am trying to find the next 10th of a month starting with today. So if today is the 27th of May, the next 10th is the 10th of June. If today is the 1st of August, the next 10th will be the 10th of August for example.
I know I can find the first day of the next month using
$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));
Can I use that format for my case as well? If not, how could I achieve my goal?
Thanks a lot for helping! :)
strtotime
is not able to do this in one call, but you could do something like:
<?php
$current_day = (int)date('j');
if ($current_day < 10) {
$firstDayNextMonth = date('Y-m-d', strtotime('+9 days', strtotime('first day of this month')));
} else {
$firstDayNextMonth = date('Y-m-d', strtotime('+9 days', strtotime('first day of next month')));
}
echo $firstDayNextMonth;
?>
Or simply just
$firstDayNextMonth = date('Y-m-d', strtotime('+9 days', strtotime('first day of ' . ((int)date('j') < 10 ? 'this' : 'next' ) . ' month')));
You can use the DateTime class:
$Date = new DateTime('first day of '.(date('j') < 10 ? 'this' : 'next').' month');
$Date->add(DateInterval::createFromDateString('9 days'));
var_dump($Date->format('Y-m-d'));
This will result in '2015-07-10' if it's '2015-06-10' today. Just use <=
(or < 11
) if it should return '2015-06-10' in that case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With