How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easiest way is to use
date, which lets you mix hard-coded values with ones extracted from a timestamp. If you don’t give a timestamp, it assumes the current date and time.date()searches the string it’s given, like'm-t-Y', for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:Ygives you the 4-digit year from the timestamp (‘2010’)mgives you the numeric month from the timestamp, with a leading zero (’04’)tgives you the number of days in the timestamp’s month (’30’)You can be creative with this. For example, to get the first and last second of a month:
See http://php.net/manual/en/function.date.php for other symbols and more details.