在 php 中一个月的最后一天的情况下的 mktime 函数

mktime function in case of Last day of a month in php

理解PHP函数中一个月的最后一天mktime

echo date("Y-m-d H:i:s", mktime(0, 0, 0, 2, 0, 2014));

输出为

2014-01-31 00:00:00

应该是

2014-02-28 00:00:00

我这里哪里做错了?

我没有看到问题。在我看来是正确的。

您要求的是 2 月 0 日,也就是 2 月 1 日的前一天,也就是 1 月 31 日。

如果您将日期设置为 0,它将 return 月份的最后一天 - 1

<?php
     $lastday = mktime(0, 0, 0, 3, 0, 2000);
     echo strftime("Last day in Feb 2000 is: %d", $lastday);
     $lastday = mktime(0, 0, 0, 4, -31, 2000);
     echo strftime("Last day in Feb 2000 is: %d", $lastday);
 ?>
echo date("Y-m-d H:i:s", mktime(0, 0, 0, 2, 28, 2014)); //You can't show 29 in feb 2014

OUTPUT : 2014-02-28 00:00:00

在 PHP 函数的 mktime 方法中将 month 第 4 个参数和第 5 个参数作为 day = 0 传递时的主要混淆。

来自PHP官方文档:

Example #3 Last day of a month**

The last day of any given month can be expressed as the "0" day of the next month, not the -1 day. Both of the following examples will produce the string "The last day in Feb 2000 is: 29".

$lastday = mktime(0, 0, 0, 3, 0, 2000);
echo strftime("Last day in Feb 2000 is: %d", $lastday);
$lastday = mktime(0, 0, 0, 4, -31, 2000);
echo strftime("\nLast day in Feb 2000 is: %d", $lastday);
// For Feb 2014
$lastday = mktime(0, 0, 0, 3, 0, 2014);
echo strftime("\nLast day in Feb 2014 is: %d", $lastday);

$lastday = mktime(0, 0, 0, 4, -31, 2014);
echo strftime("\nLast day in Feb 2014 is: %d", $lastday);
?>

输出如下:

Last day in Feb 2000 is: 29
Last day in Feb 2000 is: 29
Last day in Feb 2014 is: 28
Last day in Feb 2014 is: 28