Home  >  Article  >  Backend Development  >  PHP calculates the first and last day of a month_PHP Tutorial

PHP calculates the first and last day of a month_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:16:24903browse

To count the first and last days of a month in PHP, we can use the strtotime function to convert. Here are two methods for your reference.

The strtotime function is mainly used, it is powerful and supports relative dates

The code is as follows
 代码如下 复制代码

//计算本月第一天和最后一天
$today=date("Y-m-d");
//这个不需要解释吧,直接把第一天设为1号
$firstday = date('Y-m-01', strtotime($today));
//这个语法以前还真没见过,
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
 
echo $firstday."
";
echo $lastday;

Copy code
//Calculate the first and last day of this month
$today=date("Y-m-d");
//No need to explain this, just set the first day to No. 1
$firstday = date('Y-m-01', strtotime($today));
//I’ve never seen this syntax before,
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));

echo $firstday."
";
echo $lastday;

http://www.bkjia.com/PHPjc/628674.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/628674.html
TechArticle
To count the first and last days of a month in PHP, we can use the strtotime function to convert, as described below There are two methods for your reference. Mainly used strtotime function, function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn