Search code examples
phptimestampunix-timestampphp-carbon

Get difference between 2 date_time in minutes


Im trying to get the difference between 2 differente dates in minutes, but is not outputting correctly.

Ex:

$then = "2017-01-23 18:21:24";

//Convert it into a timestamp.
$then = strtotime($then);
//Get the current timestamp.
$now = time();

//Calculate the difference.
$difference = $now - $then;

//Convert seconds into minutes.
$minutes = floor($difference / 60);

echo $minutes;

Is outputting 611 minutes, and is wrong since from "2017-01-23 18:21:24" to "2017-01-24 12:36:24" it past much more than 611 minutes. Is my code incorrect?


Solution

  • Try to set your default timezone

    date_default_timezone_set('Europe/Copenhagen');
    

    Ofc change Europe/Copenhagen for the one that suits your needs.