Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 396
BastienSander Avatar asked May 19 '26 04:05

BastienSander


2 Answers

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.

like image 71
dynamic Avatar answered May 20 '26 17:05

dynamic


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

like image 45
vascowhite Avatar answered May 20 '26 17:05

vascowhite



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!