phpdatetimeyear2038

php - date reset to 1970 at 2038 while adding in a loop


I have been trying to generate a list of years and months based on some integer value provided by user in my PHP based web app. While doing this I faced the year 2038 bug, the year got reset to 1970 when the loop reached to 2038. The PHP version I am using is 5.4 and I googled and applied the solution found in this link, but it is not working for me, did anyone try some other solution for the 2038 bug in php?

Here is my PHP code:

 $i = 1;
 $Tenurein = 360;
 while ($i < $Tenurein) {
    $MonthArr[] = date('F', strtotime($date2));
    $YearArr[] = date('Y', strtotime($date2));
    $date = new DateTime($date2);
    $date->add(new DateInterval('P1M'));
    $dateMon = $date->format('Y-m-d');
    $date2 = $dateMon;
    $i++;
}

.


Solution

  • I'm using php version 5.6. Below code producing output View output

    <?php
    $i = 1;
     $Tenurein = 360;
     $date2 = date('Y-m-d');
     while ($i < $Tenurein) {
        $MonthArr[] = date('F', strtotime($date2));
        $YearArr[] = date('Y', strtotime($date2));
        $date = new DateTime($date2);
        $date->modify('+1 month');
        echo $dateMon = $date->format('Y-m-d');
        $date2 = $dateMon;
        $i++;
        echo "<br>";
    }
    ?>
    

    We could see lot of posts regarding this issue. For displaying date it seems there are no issues. But while storing in database i too faced same issue. if you use timestamp data type just change as datetime. It will work.