tclogo



HotelsCombined.com

Pages

Archives

Categories


Recent Postings

Recent Comments

Feeds

RSS2 feed

Links

Most Popular Posts

Getting first and last day of the month

Saturday, 31st March 2007 11:30pm
Earlier today I was working on this form with two textboxes which had to initially show the first and last day of the current month respectively.


So I wanted a compact way of doing this using PHP without using a function or method and show the date in format d/m/yyyy. The first day of the month is straight forward and I could have used something simple like this:


$dtFirstDay = "1/" . date("m") ."/" . date("Y") ;


This could have done it but in the end I settled for a method with similar formulations for both first day and last day and hence:


$dtFirstDay = date("j/n/Y", mktime(0, 0, 0, date("m") , date("d")-date("d")+1, date("Y")));


and for last day:


$dtLastDay = date("j/n/Y", mktime(0, 0, 0, date("m")+1 , date("d")-date("d"), date("Y")));

gary wrote on Sunday, 6th May 2007 7:44am
Thanks for the code snippets, exactly what I was looking for.
reply

Wes wrote on Friday, 8th June 2007 8:41pm
Good to hear that the code snippets helped you Gary. Cheers.
reply

Michael wrote on Tuesday, 3rd July 2007 4:49am
Me to! Nice snippet.
reply

Erik Larsson wrote on Monday, 7th April 2008 5:44pm
Thx bro! This helped me alot :)
reply

Jim wrote on Wednesday, 30th April 2008 2:50am
Nice. Saved me some time! One note: for the last-day-of-the-month calculation, you could just put 0 instead of date("d")-date("d") . This saves 2 calls to date().
reply

Wes wrote on Wednesday, 30th April 2008 5:54am
Thanks Jim for the hint on the last day.
reply

Roger wrote on Friday, 18th July 2008 5:47pm
Thx Wes - this is really wonderful code snippet indeed. Thanks to you millions.
reply

Francisco Trujilho wrote on Friday, 31st October 2008 10:05pm
Hi, a lot of PHP developers look for this so that code can be better.


The first day ever be "1";
$dtFirstDay = date("j/n/Y", mktime(0, 0, 0, date("m") , 1, date("Y")));


and for last day:


$dtLastDay = date("j/n/Y", mktime(0, 0, 0, date("m")+1 , 0, date("Y")));


This way we won't spend time to

"date("d")-date("d")";
reply

Wes wrote on Sunday, 2nd November 2008 10:06am
Thanks Francisco for your contribution, it think it will definitely help other Php developers.
reply

santhosh wrote on Friday, 30th January 2009 5:19pm
Thanks for the code, this is what i exactly wanted.
reply

Wes wrote on Sunday, 1st February 2009 7:37am
Good to hear that you found this useful Santhosh.
reply

homer wrote on Thursday, 26th February 2009 2:02pm
thank you, i'm searching and asking this scrip t in the forum and no one even bother to reply my question. no i found it in here.
(",)

thanks
reply

IP PBX wrote on Saturday, 20th March 2010 8:45am
Thanks for the tips, I ended up using your code today. It works nicely, its really hard to find these random things when you need something really specific in mysql. Everyone has unique ways of handling mysql. Anyways, thanks for the code snippet.
reply

blestab wrote on Wednesday, 14th April 2010 8:48pm
your script saved me precious time, thanks a lot
reply

dean wrote on Saturday, 6th November 2010 4:33am
Thanks for this one! Just what I needed.
reply

Ohana wrote on Monday, 11th July 2011 9:18pm
Thanks for the post,exactly what I was looking for.
reply

Christian wrote on Wednesday, 9th January 2013 8:41pm
Nice approach but, for the month you should use date("n") and for the day (if you don't want to remove it) date("j"). Otherwise you might end up with octal values as m & d contain leading zeros.
reply

Post a comment:

 

(required)

(required, but not published)

(optional)





Notify me of follow-up comments via e-mail