如何使用字符串减去另一个字符串来计算剩余时间


how to calculate time left using string minus another string?

我有一个$value = 5;,valnue表示5分钟,我在服务器上保存了一个文件,并被修改了很多,名为check.txt。我想要一个代码来计算文件的时间修改H:i:s中的0,从5分钟的主$value开始,然后继续,否则echo请等待从现在开始剩余的分钟-filetimemodification of the main value of 5 minutes=$timeleft,以m:s格式。

我正在测试当前的代码,但我一直得到-1376352747 的值

我的代码是坏的:)是

$filename = 'check.txt';
$time = date("H:i:s");
$time = str_replace("00", "24", $time);
$filemodtime = filemtime($filename);
$timeleft = $time - $filemodtime;
$h = explode(':', $time);
$h = $h[0];
$h = str_replace("00", "24", $h);
$m = explode(':', $time);
$m = $m[1];
$s = explode(':', $time);
$s = $s[2];
$hms = ("$h:$m:$s");
if (count($filemodtime - $time) <= 0) {
echo "you can continue";
}
else {
echo " please wait $timeleft";
}

提前感谢

filemtime()函数返回以秒为单位的UNIX时间戳,time()函数返回当前时间作为UNIX时间戳。因此,通过使用该差异,您可以以秒为单位获得文件的年龄。

$age = time() - filemtime($filename);
// if older then 5 minutes (5 * 60 secounds)
if($age > $value*60)
{
    // good
}
else
{
   $time_left = $value * 60 - $age;
   $time_left_secounds = $time_left % 60;
   $time_left_minutes = ($time_left - $time_left_secounds) / 60;
   $formated_time_left = sprintf("%02d:%02d", $time_left_minutes, $time_left_secounds);
   echo "Please wait {$formated_time_left}";
}

我建议使用time()而不是date()。这样,您就可以从currenttime()函数中减去文件时间,看看它是否大于5分钟*60秒。

祝你好运!