Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 833
lakshmen Avatar asked Nov 27 '25 00:11

lakshmen


2 Answers

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

echo $difference/60/1000;
like image 134
Object Manipulator Avatar answered Nov 29 '25 14:11

Object Manipulator


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;
like image 38
borfast Avatar answered Nov 29 '25 14:11

borfast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!