Carbon: Failed to parse time string

Created on 7 Mar 2018  路  9Comments  路  Source: briannesbitt/Carbon

Hi! I'm using php 5.6.33 and latest version of Carbon.
I'm simply instantiating a new Carbon object with new Carbon(); and one exception is raised in Carbon constructor:

DateTime::__construct(): Failed to parse time string (2018-03-07 11:47:40.-513765) at position 24 (7): Unexpected character in vendor\nesbot\carbon\src\Carbon\Carbon.php:433.

This error caused by f863708fa1b5c777e80e8dafa0441683dff260ff commit.

Should this php version also be handled before adding the microseconds?

Thanks.

All 9 comments

You must do a bit more than new Carbon() since the simple construction does not fail in our unit tests: https://github.com/briannesbitt/Carbon/blob/1.23.0/tests/Carbon/ConstructTest.php#L23

Please add more contextual code (what you pass to setTestNow() for example if you use it). The best would be a minimal case to reproduce the exact same error.

Right now if I do just what you say:

<?php

include 'vendor/autoload.php';

new \Carbon\Carbon();

It does not fail.

Thanks,

Hi, I'm the same issue using laravel 4.2, php 5.6 and the lastest version of Carbon with the f863708 commit.

In particular this is the code that goes wrong in Middleware.php of framework:

protected function getCookieLifetime()  {
        $config = $this->manager->getSessionConfig();
        return $config['expire_on_close'] ? 0 : Carbon::now()->addMinutes($config['lifetime']);
    }

@kylekatarnls what version of php did you use for the test?

Anyway, this error does not happen every time, only in some specific cases. Take the test with the date that is in the error I reported above.

<?php

include 'vendor/autoload.php';

new \Carbon\Carbon('2018-03-07 11:47:40.-513765');

Timezone used is America/Sao_Paulo

Where did you get that negative microseconds part?
This is not Carbon issue. If you use native DateTime it will fail on all versions on php: https://3v4l.org/iLuHn

I found the location of the error, it is in this line of code:

\Carbon::now()->addMonth()->gte(\Carbon::createFromFormat('Y-m-d', '2018-02-20'))

What's even weirder is that it does not happen every time.

@Glavic the following code, taken from the commit I mentioned, returns negative microseconds
```php
str_pad(strval(microtime(true) * 1000000 % 1000000), 6, '0', STR_PAD_LEFT)

I'm closing this as it's a bug related to php, probably because I'm using a experimental version (Win32 x64).
Thank you all for your attention.

I agree with that, if microtime(true) returns a negative value, it's a bug from the PHP program you use.

If you find an other code we can run to reproduce with a official release of PHP (the ones that run on Travis for example), we will inspect.

@nepos87 It seems not to be the same issue and Carbon::now()->addMinutes() works fine in our unit tests on all PHP versions:
https://github.com/briannesbitt/Carbon/blob/master/tests/Carbon/DiffTest.php#L711

So the code you pointed is not the problem I guess, it can be a bad input in $config['lifetime'] or a Carbon override made by Laravel or a third-party library (search for Carbon::macro() maybe).

If you still have troubles, please open a new issue for it with detailed code, variables values and stack trace.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings