ey dee ey em
ey dee ey em

Reputation: 8633

How to find last day of specific month of specific year with strtotime() in php

I have month in number (i.e. 1,2,3...12) I have year in number (2015,2014...2010)

What I try to do is that using strtotime() to find

strtotime("last day of 2 month 2021");

in this case, 2 and 2012 will be variable in a number listed in 1st and 2nd sentence.

I wonder is that possible to do? Because I tried the one I wrote here, its not getting the right date after I test the result of $at = strtotime("last day of 2 month 2021"); with $time = date('Y-m-d', $at); it always seems sending me the date of the current year. in the above example, result of date('Y-m-d', $at); will be

2015-04-30

what I expect to get is

2021-02-28

Anyone?

Upvotes: 0

Views: 943

Answers (2)

DevDonkey
DevDonkey

Reputation: 4880

Its really easy to find a leap year. Which really seems to be what you're trying to do.

if(2012%4 == 0)  {
    // it's a leap year
} 

Upvotes: 0

Whoami
Whoami

Reputation: 326

Try DateTime object

 $date = new DateTime('2021-02');
 $date->modify('last day of this month');
 echo $date->format('Y-m-d');

Also, the last day of 2021 Feb is 28...

Upvotes: 4

Related Questions