BastienSander
BastienSander

Reputation: 1848

strtotime return false while converting date string

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 ??

Upvotes: 3

Views: 1287

Answers (3)

vascowhite
vascowhite

Reputation: 18430

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

Upvotes: 2

dynamic
dynamic

Reputation: 48141

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.

Upvotes: 4

Manish Shrivastava
Manish Shrivastava

Reputation: 32070

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.

Upvotes: 1

Related Questions