0

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++;
}

.

3
  • 1
    This is a well known problem en.wikipedia.org/wiki/Year_2038_problem Commented Jun 3, 2016 at 12:36
  • correct, but is there any workaround in PHP to overcome this? Commented Jun 3, 2016 at 12:43
  • 1
    use DateTime objects, that can handle less / more then unix timestamp Commented Jun 3, 2016 at 13:22

1 Answer 1

1

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.

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

1 Comment

problem is I missed to change strtotime to DateTime at some other places as well, now it is working, thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.