October 26 2013

php get first and last date of the month

Tagged Under :

php
My application always need get first and last month of the date when user given a date to my system. it look like very trouble but actually quite easy only.

From here I show you how to get first and last of the date in current month.
//last day of the month
date("Y-m-t");
//first day of the month
date("Y-m-1");
Why using t in last day of the month, it is because in PHP date function “t” will return you Number of days in the given month.

And now we try get first and last date of the month from the last month.

Same as the example above, but this time need add strtotime function inside the date like example below:
date("Y-m-t", strtotime("first day of previous month"));
date("Y-m-1", strtotime("first day of previous month"));

Make a Comment

You must be logged in to post a comment.