Mazino S Ukah Mazino S Ukah - 2 months ago 14
PHP Question

Subtract Timestamps in laravel with Carbon giving error

I'm Trying to subtract 2 timestamps with Carbon but i'm getting error :
"InvalidArgumentException in Carbon.php line 425:
Hour can not be higher than 12 "

$s = \Carbon\Carbon::now();
$lastlog = \Carbon\Carbon::createFromFormat('y-m-d h:m:i', '16-09-19 14:10:11');
$h = $lastlog->diffForHumans($s);


i have also tried

$s = \Carbon\Carbon::now();
$lastlog = \Carbon\Carbon::createFromFormat('y-m-d h:m:i', '16-09-19 14:10:11');
$lastlog = date('y-m-d a h:m:i' , strtotime($lastlog));
$h = $lastlog->diffForHumans($s);

Answer

The date format for 24-hour times is H, not h: http://php.net/manual/en/function.date.php

So you want

 $lastlog = \Carbon\Carbon::createFromFormat('y-m-d H:m:i',  '16-09-19 14:10:11');
Comments