php - 还剩几秒?

发布于 2024-10-07 10:19:42 字数 454 浏览 5 评论 0原文

我试图格式化一个日期来表示还剩多少时间(以可读的格式):

<?php
    $seconds = 23414;
    $date = new DateTime();
    $date->setTime(0, 0, $seconds);
    echo $date->format('z G:i:s');
?>

这个例子可能输出类似:344 11:46:45不是我想要的。它应该显示类似于 6 days, 4:12:36 的内容。我只是在这里没有看到任何内容: http://www.php.net/ Manual/en/function.date.php 这将帮助我正确格式化它。有想法吗?

I'm trying to format a date to say how much time is left (in a readable format) from how many seconds are left:

<?php
    $seconds = 23414;
    $date = new DateTime();
    $date->setTime(0, 0, $seconds);
    echo $date->format('z G:i:s');
?>

This example might output something like: 344 11:46:45 which is not what I'd like. It should say something like 6 days, 4:12:36. I just don't see anything here: http://www.php.net/manual/en/function.date.php that would help me format it correctly. Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-10-14 10:19:42

我不知道任何内置的东西,但它很容易编写:

function formatSeconds($secondsLeft) {

  $minuteInSeconds = 60;
  $hourInSeconds = $minuteInSeconds * 60;
  $dayInSeconds = $hourInSeconds * 24;

  $days = floor($secondsLeft / $dayInSeconds);
  $secondsLeft = $secondsLeft % $dayInSeconds;

  $hours = floor($secondsLeft / $hourInSeconds);
  $secondsLeft = $secondsLeft % $hourInSeconds;

  $minutes= floor($secondsLeft / $minuteInSeconds);

  $seconds = $secondsLeft % $minuteInSeconds;

  $timeComponents = array();

  if ($days > 0) {
    $timeComponents[] = $days . " day" . ($days > 1 ? "s" : "");
  }

  if ($hours > 0) {
    $timeComponents[] = $hours . " hour" . ($hours > 1 ? "s" : "");
  }

  if ($minutes > 0) {
    $timeComponents[] = $minutes . " minute" . ($minutes > 1 ? "s" : "");
  }

  if ($seconds > 0) {
    $timeComponents[] = $seconds . " second" . ($seconds > 1 ? "s" : "");
  }

  if (count($timeComponents) > 0) {
    $formattedTimeRemaining = implode(", ", $timeComponents);
    $formattedTimeRemaining = trim($formattedTimeRemaining);
  } else {
    $formattedTimeRemaining = "No time remaining.";
  }

  return $formattedTimeRemaining;

}

我还没有彻底测试它,但我运行的测试运行良好。在使用它之前,您可能想自己测试一下。

I don't know of anything built in, but it's easy enough to write:

function formatSeconds($secondsLeft) {

  $minuteInSeconds = 60;
  $hourInSeconds = $minuteInSeconds * 60;
  $dayInSeconds = $hourInSeconds * 24;

  $days = floor($secondsLeft / $dayInSeconds);
  $secondsLeft = $secondsLeft % $dayInSeconds;

  $hours = floor($secondsLeft / $hourInSeconds);
  $secondsLeft = $secondsLeft % $hourInSeconds;

  $minutes= floor($secondsLeft / $minuteInSeconds);

  $seconds = $secondsLeft % $minuteInSeconds;

  $timeComponents = array();

  if ($days > 0) {
    $timeComponents[] = $days . " day" . ($days > 1 ? "s" : "");
  }

  if ($hours > 0) {
    $timeComponents[] = $hours . " hour" . ($hours > 1 ? "s" : "");
  }

  if ($minutes > 0) {
    $timeComponents[] = $minutes . " minute" . ($minutes > 1 ? "s" : "");
  }

  if ($seconds > 0) {
    $timeComponents[] = $seconds . " second" . ($seconds > 1 ? "s" : "");
  }

  if (count($timeComponents) > 0) {
    $formattedTimeRemaining = implode(", ", $timeComponents);
    $formattedTimeRemaining = trim($formattedTimeRemaining);
  } else {
    $formattedTimeRemaining = "No time remaining.";
  }

  return $formattedTimeRemaining;

}

I haven't tested it thoroughly, but the tests I did run worked fine. You might want to test it yourself a bit before using it.

丑丑阿 2024-10-14 10:19:42

类似于:

$SECONDS_IN_MINUTE = 60;
$SECONDS_IN_HOUR = $SECONDS_IN_MINUTE * 60;
$SECONDS_IN_DAY = $SECONDS_IN_HOUR * 24;

$days = 0;
$hours = 0;
$minutes = 0;
$seconds = 23414;

while ($seconds > $SECONDS_IN_DAY) {
   $days++;
   $seconds -= $SECONDS_IN_DAY;
}
while ($seconds > $SECONDS_IN_HOUR) {
   $hours++;
   $seconds -= $SECONDS_IN_HOUR;
}
while ($seconds > $SECONDS_IN_MINUTE) {
   $minutes++;
   $seconds -= $SECONDS_IN_MINUTE;
}

echo ($days > 0 ? $days . ' day' . ($days > 1 ? 's' : '') . ', ' : '') . 
     $hours . ':' . str_pad($minutes, 2, '0',  STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0',  STR_PAD_LEFT);

很脏,但应该可以用。

Something like :

$SECONDS_IN_MINUTE = 60;
$SECONDS_IN_HOUR = $SECONDS_IN_MINUTE * 60;
$SECONDS_IN_DAY = $SECONDS_IN_HOUR * 24;

$days = 0;
$hours = 0;
$minutes = 0;
$seconds = 23414;

while ($seconds > $SECONDS_IN_DAY) {
   $days++;
   $seconds -= $SECONDS_IN_DAY;
}
while ($seconds > $SECONDS_IN_HOUR) {
   $hours++;
   $seconds -= $SECONDS_IN_HOUR;
}
while ($seconds > $SECONDS_IN_MINUTE) {
   $minutes++;
   $seconds -= $SECONDS_IN_MINUTE;
}

echo ($days > 0 ? $days . ' day' . ($days > 1 ? 's' : '') . ', ' : '') . 
     $hours . ':' . str_pad($minutes, 2, '0',  STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0',  STR_PAD_LEFT);

Dirty, but should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文