Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php time()计算剩余时间_Php_Time - Fatal编程技术网

使用php time()计算剩余时间

使用php time()计算剩余时间,php,time,Php,Time,我有一个php脚本,我需要确保预设的“未来”时间没有过去 当时间最初被记录(或经过并需要重新记录)时,我正在进行: $newTime = time() + 15000; // 2.5 minutes from "now" 系统在数据库中抛出此项没有问题,数字似乎是正确的 现在,当加载页面时,它从数据库中提取数字并将其加载到.php文件中: error_reporting(E_ALL); ini_set('display_errors',1); $tname = $_SESSION['usern

我有一个php脚本,我需要确保预设的“未来”时间没有过去

当时间最初被记录(或经过并需要重新记录)时,我正在进行:

$newTime = time() + 15000; // 2.5 minutes from "now"
系统在数据库中抛出此项没有问题,数字似乎是正确的

现在,当加载页面时,它从数据库中提取数字并将其加载到
.php
文件中:

error_reporting(E_ALL);
ini_set('display_errors',1);
$tname = $_SESSION['username']."Data";
$results = $conn->query("SELECT val FROM $tname where pri='pettyTimer'") or die(mysqli_error($conn)); 
  //$conn declared elsewhere for connection and does work properly
$row = $results->fetch_assoc();
$timer = $row['val'];
然后我比较一下时间:

$now = time();
if ($timer > time()) { //script below
} else {
//more script that seems to be working fine
}
当原始条件
$timer>time()
为真时,我将尝试分解剩余时间的分和秒,并以用户可读的基本格式进行响应:

$raw = ($timer - $now);
$minutesLeft = floor($raw / 60000);
$totalMinutes2Mils = $minutesLeft * 60000;
$totalRemainingSecs = round(($raw - $totalMinutes2Mils) / (1000));

echo "You are still laying low from the last job you ran. You still have ".$minutesLeft." Minutes and ".$totalRemainingSecs." Seconds left.";
我的问题是,刷新/重新加载页面时,时间似乎没有改变

我回显了
time()
$timer
,它们在我第一次加载它时相隔15000毫秒,所以这应该只存在2.5分钟左右(条件为true),但从上次设置开始,我至少工作了5分钟,现在仍然是14秒

有人能再检查一下我的数学,确保我的计算正确吗?谢谢

time()函数以秒为单位返回自Unix纪元(1970年1月1日00:00:00 GMT)以来的当前时间

您将其视为毫秒,但应将其视为连续秒。大约1/1000,你会没事的

$minutesLeft = floor($raw / 60);
$totalMinutes2Mils = $minutesLeft * 60;

$newTime = time() + (60*2.5); // 2.5 minutes from "now"

time()返回秒,而不是毫秒,因此您应该添加150而不是15000以获得2:30分钟。

time()
以秒为单位返回时间戳,而不是毫秒。即使考虑到这一点,你的算术也是无处不在。15000毫秒是15秒,而不是2.5分钟。15000秒是4小时多一点。谢谢你的链接-我不知道为什么我认为是ms而不是秒。。我能接受的时候就接受。