Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dru's avatar
Level 3

getting weird date with carbon

I have extracted from the database this date: "2020-01-17 19:16:25" which is a string. and when I try to convert it to find the difference in seconds to the actual time, I get this weird output:

    $time = Carbon::createFromFormat('Y-m-d H:m:i',$time[0]->fecha_fin, 'America/Santiago'); -> this is the output   date: 2021-04-17 19:25:00.0 America/Santiago (-04:00) for some reason.

keep in mind that in app.php I have configured this

 'timezone' => 'America/Santiago',

how can I fix this to get the correct time:

dump($time->diffInDays(Carbon::now()));
0 likes
3 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@dru Try this please:

$date = Carbon::createFromFormat('Y-m-d H:i:s', "2020-01-17 19:16:25", 'America/Santiago');

The format is wrong that you use for the time.

1 like
dru's avatar
Level 3

thanks, that was it! I though I had the format right.

manjumjn's avatar

If timezone is same then you can simply use Carbon::parse($datetime); and use to differentiate

Please or to participate in this conversation.