如何计算unix时间戳之间的时差?

kht*_*kht 6 php

我用PHP创建时间戳time();

我有$current_time$purchase_time.如何确保purchase_time小于当前时间的24小时?

Ren*_*Pot 9

如果它们是UNIX时间戳,那么你可以自己很容易地计算它,因为它们是秒.

$seconds = $current_time - $purchase_time
$hours = floor($seconds/3600);
if ($hours < 24){
    //success
}
Run Code Online (Sandbox Code Playgroud)


phi*_*hag 7

由于UNIX时间戳只是秒数,所以只需使用差异:

$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';
}
Run Code Online (Sandbox Code Playgroud)