Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7.0 mktime not working

Tags:

php

getting months using mktime not working in PHP 7.0.

    $month_options="";
    for( $i = 1; $i <= 12; $i++ ) {
        $month_num = str_pad( $i, 2, 0, STR_PAD_LEFT );
        $month_name = date( 'F', mktime( 0, 0, 0, $i + 1, 0, 0, 0 ));
        $selected="";
        $month_options.$month_name."<br/>";
    }
    echo $month_options;

Result in PHP 5.5

January
February
March
April
May
June
July
August
September
October
November
December

Result in 7.0

January
January
January
January
January
January
January
January
January
January
January

please help me how to reslove this issue?..thanks in advance

like image 351
sridhard Avatar asked Jun 24 '26 08:06

sridhard


2 Answers

It is cleary written here that last parameter is_dst of mktime has been removed in PHP 7, You have to give 6 parameters instead of 7.

Try this code snippet here 7.0.8

<?php

ini_set('display_errors', 1);
$month_options = "";
for ($i = 1; $i <= 12; $i++)
{
    $month_num = str_pad($i, 2, 0, STR_PAD_LEFT);
    $month_name = date('F', mktime(0, 0, 0, $i + 1, 0, 0));
    $selected = "";
    $month_options .= $month_name . "<br/>";
}
echo $month_options;
like image 146
Sahil Gulati Avatar answered Jun 25 '26 22:06

Sahil Gulati


Why not use DateTime objects instead? They are easier to work with, and easier to manipulate. DateTime is available from PHP5.2 and upwards.

This snippet

$date = new DateTime("january");
for ($i = 1; $i <= 12; $i++) {
    echo $date->format("F")."\n";
    $date->modify("+1 months");
}

would output

January
February
March
April
May
June
July
August
September
October
November
December

Live demo

like image 30
Qirel Avatar answered Jun 25 '26 20:06

Qirel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!