Epistomai
Epistomai

Reputation: 475

penultimate day of month in php

So far with

strtotime('last day of this month');

gives me the last day (31), but I want the penultimate (30), and I've tried with:

strtotime('-1 day','last day of this month');

Also with strtotime('last day of this month') - strtotime('1 day'); and other things. Always results in (formatted with date()) 31-dec-1969.

How can I achieve this?

Upvotes: 1

Views: 230

Answers (1)

S Raghav
S Raghav

Reputation: 1526

You're almost there. strtotime() expects parameter 2 to be an integer in case of subtracting a time span from a date. So you need to

  1. convert the last day of the month to an integer timestamp value using strtotime('last day of this month')
  2. Then subtract a day from it by doing strtotime('-1 day', $valueFromStep1)

Hence doing

strtotime('-1 day',strtotime('last day of this month'))

Gives the expected answer

Here is a similar question to the one you have asked: Subtract 1 day with PHP

strtotime reference: http://php.net/manual/en/function.strtotime.php

Upvotes: 2

Related Questions