John Higgins John Higgins - 1 year ago 58
PHP Question

Get last 1st November date

I am trying to get the last 1st November date based on today's date.

E.g. today is 2016-09-23 so the last 1st November would be 2015-11-01.

If today's date is 2016-11-20 the last 1st November would be 2016-11-01.

I have the following code which just pulls the 1st November of the current year.

$last_november = date('Y-m-d', strtotime('1st november'));

Can anyone help?



Answer Source

Use this :

$date = date('Y-m-d');
$lastNovember = date('Y-m-d', strtotime('1st november'));
$year = '';
//decrement the year's value by 1 to get previous year november's first date
if ($date < $lastNovember) {
  $year = date('Y') - 1;
  $lastNovember = date('Y-m-d', strtotime("1st november {$year}"));


i.e, use previous year if current date is less than this year's Nov's date

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download