Yday I was working with dates and got the situation to get the last day of the month so got this piece of code.
date(’Y-m-d’,strtotime(’-1 second’,strtotime(’+1 month’,strtotime(date(’m').’/01/’.date(’Y').’ 00:00:00′))));
And this piece of code will gv you the last day of the your corrent month.
tags: PHP, code, month, programming
Simply Great Script.
How do it for the last month one
great! thx!
to get the last month,
date(‘Y-m-d’,strtotime(‘-1 second’,strtotime(date(‘m’).’/01/’.date(‘Y’).’ 00:00:00′)));
the output will bre the last day of the month for last month.
Hi
I was thinking to write a complete function and then googled your this post. I cant believe you did it with one line.
great work thank you.
Natasha
Just for the record -
This here is a bit shorter and thus better readable:
date(“Y-m-d”,mktime(0, 0, 0, (date(‘m’) + 1), 0, date(‘Y)))
Shorter != better readable
Many times more efficient as well, date and mktime are highly optimised functions, strtotime is not.
Thanks for this!
date(‘t’) is enough to get the last day of the month.
Slight modificatiion of your answere
For getting last date of any month
date(“t”,$tm);
where $tm is the time stamp of the date
Yes you r right.
incredibly simple, thank you!
You could combine all those strtotime functions:
strtotime(‘+1 month -1 second ‘.date(‘Y’).’-’.date(‘m’).’-01′ );
kumar is right to get the last day of the current month is just date(‘t’) you don’t need to use more function, remember keep it simple
I agree this is much faster and readable
date(“Y-m-t”); //t = number of days in the month
this works fine thanks buddy
cal_days_in_month() return days of month. Very handy, because format and year are parameters.
Good one. Exactly what I’m looking for. Simple, readable. Thanks.
Thank you.. thank you.. date(’t’) returns only date of the last month. But I was looking for code that return complete date for last day of the month(ex – 2000-01-31) and found it here – I used tobi’s code –
date(“Y-m-d”,mktime(0, 0, 0, (date(‘m’) + 1), 0, date(‘Y’)))
Thanks Guys… This Post just help me out to find the parameter t. I’m using date(‘t’).
Cheers,
alita
super funda yar thanks a lot i using this i have formulated many calculation on date
here is the find last month
if(date(“m”)==1)
{
$to_month= 12 ;
$to_year=date(“Y”)-1;
}
else
{
$to_month=date(“m”)-1;
$to_year=date(“Y”);
}
$totime = date(“Y-m-d”,strtotime(“-1 second”,strtotime(“+1 month”,strtotime($to_month.”/01/”.$to_year.” 00:00:00″)))).”00:00:00″;;
thank you for good code
ThanX for the functional bindings.
pooleez, just use date(‘Y-m-t’)
why all the complications
Pingback: Get the first and the last day of the current month in PHP | Svetoslav Marinov's Blog
all this seems overly complex when ‘mktime(0,0,0,date(“m”) + 1, 0, date(“Y”)));’ does the same thing. Less function calls and slightly easier to read.
mktime is very powerful, changing years or months if one value causes the them to roll over. date(“Y-m-d’,mktime(0,0,0, 2,32,2008)) will com out as 2008-03-03
I know this is old….But I cant believe i was struggling to get here:
$where[] = ‘ed1.date BETWEEN “‘.date(‘Y-m-01′, $date_ts).’” AND “‘.date(‘Y-m-t’, $date_ts).’”‘;
“t” seems so obvious now – I was trying to find some weird shortcut like “last day of month” for strtotime() to gobble up. Anyway, this post may now die in peace.
Wow! date(‘t’) is the simplest way
before i use very very hard method:
$m = date(‘m’);
$y = date(‘Y’);
//get number of days in current month
$n = ($m!=2?(($m%2)^($m>7))+30:(!($y%400)||!($y%4)&&($y%25)?29:28));
$n – is the number of the last day in month!!!
easy peasy
date(‘t’,strtotime(“last month”));
Not so sure about date(‘t’);
It looks good but
gives me
Sun 2010-05-31
but 2010-05-31 is a Monday
Not so sure about date(‘t’);
It looks good but
date(‘D Y-m-t’, strtotime(“last month”) gives me Sun 2010-05-31
but 2010-05-31 is a Monday
it works ok, but you are using it wrong. “D” and “t” are not referring to the same day.
D: returns the day of the month, one month ago from now (i.e. if current date is Wed 07 Jul 2010, it returns Mon 07 Jun 2010), but “t” returns the number of days of the previous month (i.e. 30, as june has 30 days).
(Y: returns the year of the previous month, m: returns a numeric representation of the previous month )
To get the last day of a month use this quite simple code:
$month =2;
echo date(‘Y-m-t’,mktime(0,0,0,$month,1,2010));
From Php manual …. uses day “0″ , quite nice
Example #3 Last day of next month
The last day of any given month can be expressed as the “0″ day of the next month, not the -1 day. Both of the following examples will produce the string “The last day in Feb 2000 is: 29″.
$date = new Zend_Date();
$lastday = $date->get(Zend_Date::MONTH_DAYS);
You saved my butt with this one! Apparently it’s pretty easy to do in 5.3 but not so much in 5.2
Hi, I am using this code:
date(‘Y-m-t’, strtotime(‘now’) ); // 2010-11-30 = last day of this month
date(‘Y-m-t’, strtotime(‘+1 month’) ); // 2010-12-31 = last day of next month
etc.
I needed, last day of a month, related to any date….
$b=”2011-1-05″; // any date
$c=1; // months after that date
echo date(“Y-m-t”,mktime(0,0,0,date(“m”,strtotime($b))+$c,1,date(“Y”,strtotime($b))));
// result: 2011-02-28
One more way without strtotime(), I don’t know why I don’t like it.
- It will give you the last day/second of previous month.
date(‘Y-m-d H:i:s’, mktime(0, 0, -1, $currentMonth, 1, $currentYear))
- It will give you the last day of current month
date(‘Y-m-t’, time())
… not working related to ANY DATE… that’s the problem.
Pingback: My 2010 in review – by wordpress | <!– something random just like junk – yard –>
$month = date(“m”);
$year = date(“Y”);
date(“t”, mktime(1,1,1,$month,1,$year));
This code returns the last day from a given month
I think date(‘Y-m-t’) will do the job.
Many thanks for this time saving code.
If you want to print the th, st with the last day of the month within your date function.
Like June 30th, 2011 or July 31st, 2011
Try this:
date(‘F tS, Y’,strtotime(date(‘F t’)))
great job…i will realy appreciat if you can help me with understanding this script. TY very much
Thanks 4 the script
Great post. ty for the script
Nice PHP trick, works fine for me!
Just simple code to Find last day of the month in PHP
echo date(‘Y-m-d’,strtotime(“last Day”));
Pingback: Get the last day of the month - Logan Bibby
Pingback: Code PHP สำหรับค้นหาวันสุดท้ายของเดือน | Web Development and Programming Help.
getting start date and end date of last month.
$dt = date(‘Y-m-d’);
$date_start = date(‘Y-m’, strtotime(‘-1 month’. $dt)) . ‘-01′;
$date_end = date(‘Y-m-t’, strtotime($date_last_1_month_start));
of course the above can be changed to fit any date.
getting start date and end date of last month.
$dt = date(‘Y-m-d’);
$date_start = date(‘Y-m’, strtotime(‘-1 month’. $dt)) . ‘-01′;
$date_end = date(‘Y-m-t’, strtotime($date_start));
of course the above can be changed to fit any date.
$Totaldaysforlastmonth = date(“t”, mktime(0,0,0, date(“n”) – 1));
you can also do this
date(‘Y-m-t’);
the ‘t’ will get the last day of the month
or this
echo date(‘t’, strtotime(“2012-02-01”) ); // return 29
date(‘Y-m-d H:i:s’, mktime(23,59,59,$month+1,0,$year));