在计算时间差之后,我该如何确定差值是只有分钟,还是小时,或者是天、周、月、年...?
$now = new DateTime('now');
$expire_date = date('Y-m-d H:i:s', strtotime('+ 3 weeks'));
$expire_on = new DateTime($expire_date);
$time_left = $now->diff($expires_on);
*** 伪代码如下所示,展示了我想要实现的功能
if ($time_left < 1 小时)
显示分钟
elseif ($time_left < 1 天)
显示小时和分钟
elseif ($time_left < 1 周)
显示天、小时和分钟
elseif ($time_left < 1 月)
显示周、天、小时和分钟
elseif ($time_left < 1 年)
显示月、周、天、小时和分钟
endif
** 结束伪代码
正如我很快学到的那样,我需要澄清问题。我只想显示最少的信息。例如:“剩余39分钟”,而不是“剩余0天,0小时,39分钟”。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号