首页 > 解决方案 > 如何在 php 中使用 date() 显示日期和时间

问题描述

我有一个时间戳,它计算两个日期之间的剩余时间。

$job_expiration = strtotime($job['job_expiration']) - time();像这样。

现在,我只展示了 1 天或 2 天的date()功能。

date('j', $job_expiration)像这样。

我还想显示小时而不是 2 天。假设剩余时间为 1 天 16 小时或仅 30 分钟。然后我也想显示一天的时间。

1 天、16 小时或仅 30 分钟

我怎样才能做到这一点?

标签: phpdatedatetime

解决方案


function convert_seconds($seconds) {
  $dt1 = new DateTime("@0");
  $dt2 = new DateTime("@$seconds");
  return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo convert_seconds(200000)."\n";

样本输出:

2天7小时33分20秒

在您的情况下,您应该使用此功能,例如convert_seconds($job_expiration)


推荐阅读