【发布时间】:2021-04-03 09:07:54
【问题描述】:
我想检查从生成令牌开始是否已经过了 24 小时。当我创建一个令牌时,我保存当前时间戳并在使用令牌之前检查时间戳是否有效,我尝试了下面的代码但没有成功,即使时间戳超过 2 天,它也会一直显示令牌有效。
<?php
$time24Hours = 86400;
$time2Hours = 7200;
$timestamp = 1608234028;
$timezone = "Asia/Kolkata";
$currentTimestamp = (new DateTime("now", new DateTimeZone($timezone)))->getTimestamp();
if(($timestamp - $currentTimestamp) > $time24Hours){
echo "Token is more than 24hours\n";
}else{
if(($timestamp - $currentTimestamp) > $time2Hours){
echo "Token is less than 24hours but still more than 2hours required duration\n";
}else{
echo "Token is active\n";
}
}
echo date('m/d/Y H:i:s', $timestamp);
?>
我也试过了
<?php
$timezone = "Asia/Kolkata";
$timestamp = new DateTime();
$timestamp->setTimestamp(1608234028);
$timestamp->setTimezone(new DateTimeZone($timezone));
$currentTimestamp = new DateTime("now", new DateTimeZone($timezone));
if($currentTimestamp < $timestamp->modify('-24 hour')){
echo "Token is more than 24hours\n";
}else{
if($currentTimestamp < $timestamp->modify('-2 hour')){
echo "Token is less than 24hours but still more than 2hours required duration\n";
}else{
echo "Token is active\n";
}
}
echo date('m/d/Y H:i:s', $timestamp->getTimestamp());
?>
【问题讨论】:
-
你能粘贴dump
$timestamp->modify('-24 hour')的输出吗
标签: php datetime timestamp timezone