Understanding of last day of a month in the PHP function mktime
echo date("Y-m-d H:i:s", mktime(0, 0, 0, 2, 0, 2014));
Output is
2014-01-31 00:00:00
It should be
2014-02-28 00:00:00
Where is the wrong thing I am doing here?
Understanding of last day of a month in the PHP function mktime
echo date("Y-m-d H:i:s", mktime(0, 0, 0, 2, 0, 2014));
Output is
2014-01-31 00:00:00
It should be
2014-02-28 00:00:00
Where is the wrong thing I am doing here?
I don't see the problem. It looks correct to me.
You have asked for February 0, which is the day before February 1 AKA Jan 31.
if you set day to 0 it will return the last day of month - 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
Major confusion when you pass the month
4th argument and 5th argument as day = 0
in the mktime method of PHP function.
From PHP official documentation:
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);
?>
Output as below:
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