Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the time difference between unix time stamps?

Tags:

php

I am creating time stamps in PHP using time();

I have the $current_time and $purchase_time. How do I make sure that purchase_time is less than 24 hours of current time?

like image 664
kht Avatar asked Oct 15 '11 15:10

kht


People also ask

How do you calculate the difference in time stamps?

The difference is calculated by subtracting the second operand from the first. The result is rounded down, with any remainder discarded. For example, 61 minutes is equal to 1 hour, and 59 minutes is equal to 0 hours. The value returned by the function is compatible with both type numeric and type duration.

How do you find the difference in time between two timestamps?

If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

How do you find the difference between two time in Unix?

First, calculate convert the dates to be compared into unix timestamps: date1=$(date --date="2020-10-31 05:41:33" "+%s") date2=$(date --date="2020-11-01 05:41:33" "+%s")

Can you subtract Unix timestamp?

Method 1: Subtracting Unix timestampsYou can easily convert any date, in any format to a Unix timestamp using the in-built PHP function strtotime(). Now that you have the time difference in seconds, you can easily convert it to other durations of your preference such as hours, days, months, years, etc.


2 Answers

If they are UNIX timestamps, then you can calculate this by yourself really easy, as they are seconds.

$seconds = $current_time - $purchase_time
$hours = floor($seconds/3600);
if ($hours < 24){
    //success
}
like image 177
Rene Pot Avatar answered Oct 01 '22 13:10

Rene Pot


Since UNIX timestamps are just numbers of seconds, just use the difference:

$purchasedToday = $current_time - $purchase_time < 24 * 60 * 60;
if ($purchasedToday) {
  echo 'You just bought the item';
} else {
  echo 'You bought the item some time ago';
}
like image 34
phihag Avatar answered Oct 01 '22 15:10

phihag