-3

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?

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44

4 Answers4

2

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.

Ariel
  • 25,995
  • 5
  • 59
  • 69
2

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);
 ?>
donald123
  • 5,638
  • 3
  • 26
  • 23
0
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

Priyank
  • 3,778
  • 3
  • 29
  • 48
0

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
Community
  • 1
  • 1
krishna Prasad
  • 3,541
  • 1
  • 34
  • 44