Php 如何计算unix时间戳之间的时间差?
我正在用PHP使用Php 如何计算unix时间戳之间的时间差?,php,Php,我正在用PHP使用time()创建时间戳 我有$current\u time和$purchase\u time。如何确保购买时间小于当前时间的24小时?由于UNIX时间戳仅为秒数,请使用差异: $purchasedToday = $current_time - $purchase_time < 24 * 60 * 60; if ($purchasedToday) { echo 'You just bought the item'; } else { echo 'You bought
time()
创建时间戳
我有
$current\u time
和$purchase\u time
。如何确保购买时间小于当前时间的24小时?由于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';
}
$purchasedToday=$current\u time-$purchased\u time<24*60*60;
如果($purchasedToday){
echo‘你刚买了这个东西’;
}否则{
echo‘你不久前买的物品’;
}
如果它们是UNIX时间戳,那么您自己就可以很容易地计算出来,因为它们是秒
$seconds = $current_time - $purchase_time
$hours = floor($seconds/3600);
if ($hours < 24){
//success
}
$seconds=$current\u time-$purchase\u time
$hours=地板($seconds/3600);
如果($24小时以内){
//成功
}
我曾经用过这样的东西:
<?php
if(date("U", strtotime("-24 hours", $current_time) > date("U", $purchase_time)) {
echo "More then 24 hours you purchased this radio";
}
?>
$difference=time() - $last_login;
您可以构造一个对象,然后使用它的方法计算$current\u time和$purchase\u time之间的差异:
$currentTime = new DateTime();
$purchaseTime = new DateTime('2011-10-14 12:34:56');
// Calculate difference:
$difference = $currentTime->diff($purchaseTime);
if ($difference->days >= 1) {
echo 'More than 24 hours ago.';
}
这比自己计算差异更可靠,因为此方法考虑了时区和夏令时。类似于以下内容:
<?php
if(date("U", strtotime("-24 hours", $current_time) > date("U", $purchase_time)) {
echo "More then 24 hours you purchased this radio";
}
?>
$difference=time() - $last_login;