php - 还剩多少时间? PHP + 日期

标签 php date

//Example data
$current_time = 1318075950;
$unbanned_time = $current_time + strtotime('+1 minute');


if ($unbanned_time > $current_time) {
  $th1is = date('Y-m-d H:i:s', $unbanned_time) - date('Y-m-d H:i:s', $current_time);
   echo date('Y-m-d H:i:s', $th1is);

我试图输出用户被解除封禁之前的时间……年月、日、小时、分钟和秒……但这给了我一些奇怪的结果……

最佳答案

您应该查看有关如何使用日期/时间函数的手册。

首先,而不是

$current_time + strtotime('+1 minute')

使用

strtotime('+1 minute', $current_time);

(参见 strtotime 上的手册)。

其次,date函数返回一个字符串。在大多数情况下,减去两个字符串并不是很有用。

if ($unbanned_time > $current_time) {
  $th1is = $unbanned_time - $current_time;
  echo $th1is/3600 . ' hours';
}

这将以小时为单位输出剩余时间,但有许多可用的函数可以生成更好的格式(或者您可以自己编写一个)。

关于php - 还剩多少时间? PHP + 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7696715/

相关文章:

javascript - 将毫秒转换为具有 UTC 时区的 UTC 日期对象

MySQL:如何在某些条件下选择特定时间范围之间的最大/最小时间数据?

带有时间间隔的日期之间的mysql查询

javascript - zippyshare.com 直接下载链接

php - Laravel - 两次关联相同的数据透视表

php - 如何在 PHP 中创建网页图像?

python - 对日期时间数据帧进行排序和组合 Pandas Python

java - mongodb 中的日期不正确

php - 文件未上传

php - 为每个系列创建一个下拉菜单