3

Using php 5.4.34 And Laravel 4 with apache 2.2.22 and Ubuntu.

I try to convert the string '2050-10-13' to a date and it always return false.

var_dump(strtotime('2050-10-13')); ==> false
var_dump(strtotime('13-10-2050')); ==> false
var_dump(strtotime('2050/10/13')); ==> false
var_dump(strtotime('13/10/2050')); ==> false

I tried to add before :

date_default_timezone_set('Europe/Brussels');

OR

date_default_timezone_set('Europe/Paris');

I doesn't change anything.

in app/config/app.php I have :

'timezone' => 'UTC',
'locale' => 'en',

What could be the problem ??

1
  • 1
    cf the manual, in particular, the second note 32bit vs 64bit systems and the valid time ranges Commented Nov 13, 2014 at 12:48

3 Answers 3

4

2050 cannot be represented internally on 32 bit systems.
Timestamp have a limit to 2038 because the max value for a 32-bit integer is 2,147,483,647, that is: 2038-01-19T03:14:08+0000Z

You have just experienced the year2038 bug.

How to fix

Don't use timestamp. Instead use a more robust library such as new \DateTime('2050-10-13'); and of course on your Database use Date field.

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

5 Comments

2050 can be represented internally, provided you're on a 64bit system
@dynamic: a long of 4 bytes, you mean... PHP integers are represented internally by the C type long :-P (pedantic, I know)
That's right, but Integer (in english speaking) include long :)
var_dump(strtotime('13-10-1999')); => int 939762000
@BastienSander: check the default format for the timezone you're using: is it m-d-Y or d-m-Y, in the first case: 13 is not a valid month
2

The problem is that you are using a function that cannot cope with dates so far in the future on a 32 bit system.

Use DateTime instead it will cope quite happily with such dates:-

$date = new \DateTime('2050-10-13');
var_dump($date);

Demo

1 Comment

And this answers the "What could be the problem" question how, exactly?
1

Adding answer into dynamic answer. Timestamp have a limit to 2038 because The maximum value of a 32-bit integer is 2,147,483,647. If you add +1 to that, you get -2,147,483,647. 2,147,483,647 seconds from 01-01-1970 00:00:00 is January 19, 2038. If you add one more second, you get a date somewhere in 1902.

Hard luck this time.

1 Comment

Again, pedantic, but technically: signed integer overflow results in undefined behavior, so in theory, anything could happen. 99% of the time, INT_MAX + 1 == INT_MIN. In theory however, a segfault or an LPT0 printer on fire error is just as likely

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.