smartcoderx
smartcoderx

Reputation: 1081

How to get penultimate monday of the month?

I can create DateTime Objects with string parameter to get dates, like this.

$first = new \DateTime("first monday of july 2015");
$second = new \DateTime("last tuesday of july 2015");
$third = new \DateTime("first monday of january 2015");

But how can i get penultimate monday of july?

Upvotes: 1

Views: 462

Answers (4)

pinkal vansia
pinkal vansia

Reputation: 10330

$penultimateMonday = new \DateTime("last monday of july 2015 -1 week");

echo $penultimateMonday->format('Y-m-d');

Output

2015-07-20

Upvotes: 2

Lukas Jahoda
Lukas Jahoda

Reputation: 208

You can do it by DateInterval example

$last = new \DateTime("last monday of july 2015");
$penultimate = $last->sub(new \DateInterval('P7D'));

Variable $penultimate will be last Monday minus 7 days what is penultimate Monday of july

Upvotes: 1

sprague44
sprague44

Reputation: 141

The penultimate mondoay of july is always 7 days before the last monday in july, so you can do:

$last = new \DateTime("last monday of july 2015");
$penultimate = $last -7;

Upvotes: 0

Disha V.
Disha V.

Reputation: 1864

echo $test = date("Y-m-d",strtotime("last monday of july 2015"));

Upvotes: -1

Related Questions