php - 如何在 php 中使用 date() 显示日期和时间
问题描述
我有一个时间戳,它计算两个日期之间的剩余时间。
$job_expiration = strtotime($job['job_expiration']) - time();
像这样。
现在,我只展示了 1 天或 2 天的date()
功能。
date('j', $job_expiration)
像这样。
我还想显示小时而不是 2 天。假设剩余时间为 1 天 16 小时或仅 30 分钟。然后我也想显示一天的时间。
1 天、16 小时或仅 30 分钟
我怎样才能做到这一点?
解决方案
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)
推荐阅读
- shell - 循环中的 Sed 命令问题
- python - Regex similar to a Hearst Pattern in Python
- javascript - 如果 `element.style.box-sizing` 是 `border-box`,则设置 `element.style.height` 无法按预期工作
- android - 造型 OssLicensesMenuActivity
- vector - 如何对索引在 HashSet 中的向量中的元素求和?
- python - AttributeError:模块“st_gcn.feeder”没有属性“Feeder_newdataset”
- r - 如何在 docker 容器中使用 supervisord 运行 Rserve?
- backup - 在 Cumulocity Edge/Cloud 上备份我的数据
- javascript - 高阶函数 Javascript
- reactjs - NGINX React 单页站点 - 不会从子文件夹中提供 CSS