1

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.

2
  • 1
    $month=28 ~ I think that might be a few too many Commented Sep 13, 2016 at 6:30
  • 2
    I don't think a 28th month exists. Commented Sep 13, 2016 at 6:31

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah silly me, The input values ive been getting are quite unpredictable!
2

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

Comments

1

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

*/

Comments

1

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>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.