PHP PHP Gets the number of days of the month and the first and last day of the month, the first day of the month and the last day implementation method

Source: Internet
Author: User
Tags echo date

In the process of doing the query, for example, to realize the last month from the first day to the last day of Commission (Commission), then we in the program implementation process to let the program in the scope of the last month to query, the first day is relatively good, but the last day is uncertain, To write the function of the month and the year to determine the number of days before the last month. That's more than trouble. Gets the regular date of the current month, the current year, and the following code is the correct code after testing the company's engineers, you can rest assured that use.
1.get the first day and last day of the month.
echo Date (' y-m-01 ', Strtotime ('-1 month '));
echo "<br/>";
echo Date (' Y-m-t ', Strtotime ('-1 month '));
echo "<br/>";
2.get the first day of the month and the last day.
$BeginDate =date (' y-m-01 ', Strtotime (date ("y-m-d"));
Echo $BeginDate;
echo "<br/>";
echo Date (' y-m-d ', Strtotime ("$BeginDate +1 month-1 Day");
echo "<br/>";
3.get the day of the year, month, day, and days.
echo "Total this month:". Date ("T"). " Days ";
echo "Current year". Date (' Y ');
echo "Current Month". Date (' m ');
echo "Current Date". Date (' d ');
echo "<br/>";
4.use functions and arrays to get the first day of the month and the last day, more practical, from netizens.
function Getthemonth ($date)
{
$firstday = Date (' y-m-01 ', Strtotime ($date));
$lastday = Date (' y-m-d ', Strtotime ("$firstday +1 month-1 Day");
Return Array ($firstday, $lastday);
}
$today = Date ("y-m-d");
$day =getthemonth ($today);
echo "The first day of the month:". $day [0]. "Last day of the month:". $day [1];
echo "<br/>";

PHP PHP Gets the number of days of the month and the first and last day of the month, the first day of the month and the last day implementation method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.