0DDC0
0DDC0

Reputation: 5189

PHP - mktime() function returning wrong time

The Problematic Code

I am trying to build a time using some string values extracted from a file. This is basically the code that runs.

$hour = "18";            
$minutes = "0";
$month = "28";
$day = "4";
$year = "2016";
echo("<div>"."Current PHP version: " . phpversion()."</div>");  
echo("<div>hour :: ".(int)$hour."</div>");  
echo("<div>minutes :: ".(int)$minutes."</div>");        
echo("<div>month :: ".(int)$month."</div>");        
echo("<div>day :: ".(int)$day."</div>");        
echo("<div>year :: ".(int)$year."</div>");                  
$built_time = mktime((int)$hour,(int)$minutes,0,(int)$month,(int)$day,(int)$year);
echo("<div>  Built Time [Y-m-d H:i:s]: ".date( 'Y-m-d H:i:s',$built_time)."</div>");

The Wrong Results

The output is receive for this is:

Output for given code snippet

Several more attempts yielded the following results

Other attempts

As you can see sometimes it does provide the correct result! Why does this happen. It cannot be a timezone issue since its giving dates which are years into the future.

As per mktime() documentation the parameter sequence is also correct. Wonder why this call is failing.

Upvotes: 1

Views: 529

Answers (4)

Guri
Guri

Reputation: 83

try this

$built_time=strtotime($year."-".$month."-".$day." ".$hour.":".$minutes.":00");
   echo("<div>  Built Time [Y-m-d H:i:s]: ".date( 'Y-m-d H:i:s',$built_time)."</div>");

Upvotes: 1

Professor Abronsius
Professor Abronsius

Reputation: 33813

It was likely just a simple mistake but there are not 28months to a year so I guess the month and day were swapped.

$hour = "18";            
$minutes = "0";
$month = "4";
$day = "28";
$year = "2016";
$built_time = mktime((int)$hour,(int)$minutes,0,(int)$month,(int)$day,(int)$year);

echo"
<div>"."Current PHP version: " . phpversion()."</div>
<div>hour :: {$hour}</div>
<div>minutes :: {$minutes}</div>
<div>month :: {$month}</div>
<div>day :: {$day}</div>
<div>year :: {$year}</div>
<div>  Built Time [Y-m-d H:i:s]: ".date( 'Y-m-d H:i:s',$built_time)."</div>";
/*
outputs

Current PHP version: 5.3.2
hour :: 18
minutes :: 0
month :: 4
day :: 28
year :: 2016
Built Time [Y-m-d H:i:s]: 2016-04-28 18:00:00

*/

Upvotes: 1

Hanky Panky
Hanky Panky

Reputation: 46900

This is perfectly the right output. 28th month of 2016 is April 2018

Upvotes: 2

Alok Patel
Alok Patel

Reputation: 8022

The result is correct according to your inputs.

mktime accetps month value to be between 1-12. In each of your attempts except last one month values are 28,28,21,21.

So when the value of month is more then 12, it references the appropriate month in the following year(s). So in your first two case it would be 04 and and in 3rd and 4th it would be 09.

When you pass value of month greater then 12 it calculates the N months after current month. So in first two cases it's April 2018(04) and and in 3rd and 4th it's September(09) 2017.

So the outputs are correct according to your inputs.

Upvotes: 3

Related Questions