lakshmen
lakshmen

Reputation: 29064

How to find the difference between two UNIX timestamps in minutes PHP

I am trying to find the difference between the two UNIX timestamps in minutes in php.

I wrote such a code:

$current_time = 1474719107000;
$start_time = 1474716600000;
$difference = $current_time - $start_time;
echo $difference/60;

The difference should be almost 40 mins. But I am getting a value of 41783.333333333. Not sure what's my mistake.

Need some guidance on this.

Upvotes: 2

Views: 32392

Answers (2)

borfast
borfast

Reputation: 2274

I don't know if you're aware of that but those are not PHP timestamps; PHP timestamps (and Unix timestamps as well) are represented in seconds and the ones you have there seem to be milliseconds. Are those from Javascript?

Anyway, if you want to get a result in minutes using those values, you need to divide milliseconds by 1000 (to get seconds) and then divide again by 60 (to get minutes).

This should work:

$current_time = 1474719107000;
$start_time = 1474716600000;
$difference = $current_time - $start_time;
echo $difference / 1000 / 60;

Upvotes: 1

Indrasis Datta
Indrasis Datta

Reputation: 8618

Your two timestamps are in milliseconds. You need to further divide them by 1000.

echo $difference/60/1000;

Upvotes: 8

Related Questions