Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the next 10th of a month from now?

Tags:

date

php

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! :)

like image 552
Arnie Avatar asked May 27 '15 06:05

Arnie


2 Answers

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')));
like image 91
redelschaap Avatar answered Sep 18 '22 16:09

redelschaap


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.

like image 43
hchr Avatar answered Sep 22 '22 16:09

hchr