Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get last day of given month [duplicate]

Tags:

date

php

datetime

I want to output of last and first date of a given month of current year. I am using this code but not works

$month='02';
$first_day_this_month = date('Y-'.$month.'-01'); // hard-coded '01' for first day
$last_day_this_month  = date('Y-'.$month.'-t');

echo $first_day_this_month;print'<->';echo $last_day_this_month;

my output shows

2015-02-01<->2015-02-31

But it will be 2015-02-01<->2015-02-28

like image 731
Frai1989 Avatar asked Oct 15 '15 03:10

Frai1989


1 Answers

I have had this problem with PHP before, try the following way:

$dateToTest = "2015-02-01";
$lastday = date('t',strtotime($dateToTest));
like image 189
Ushal Naidoo Avatar answered Oct 13 '22 02:10

Ushal Naidoo