如何打印日期,例如:2 小时前,mar 10
function timeSpent($date_created, $endingDay)
{
$diff = abs(strtotime($date_created) - strtotime(date("Y-m-d H:i:s")));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
$minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
$seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minuts * 60));
$week = floor($days / 7);
$day = $days % 7;
if ($week < 1 && $week != 0) {
return $days . " days - " . $hours . " hrs - " . $minuts . "- min" . $week;
} elseif ($week == 0) {
if ($days != 0) {
return $days . "days " . $hours . "hours ago";
} elseif ($hours != 0) {
return $hours . "hours " . $minuts . "minute ago";
} elseif ($minuts) {
return $minuts . "minute " . $seconds . "seconds ago";
} else {
return "few seconds ago";
}
} else {
$stamp = strtotime($date_created);
return date("F d Y", $stamp);
}
}
}
我正在使用这个功能,它无法正常工作。
function timeSpent($date_created, $endingDay)
{
$diff = abs(strtotime($date_created) - strtotime(date("Y-m-d H:i:s")));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
$minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
$seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minuts * 60));
$week = floor($days / 7);
$day = $days % 7;
if ($week < 1 && $week != 0) {
return $days . " days - " . $hours . " hrs - " . $minuts . "- min" . $week;
} elseif ($week == 0) {
if ($days != 0) {
return $days . "days " . $hours . "hours ago";
} elseif ($hours != 0) {
return $hours . "hours " . $minuts . "minute ago";
} elseif ($minuts) {
return $minuts . "minute " . $seconds . "seconds ago";
} else {
return "few seconds ago";
}
} else {
$stamp = strtotime($date_created);
return date("F d Y", $stamp);
}
}
}
i am using this function it doesn't work properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到的一个错误是,如果您输入 4 月 15 日,则可能有 1 个月零 0 周。这将导致仅输出日期和小时。更改以下内容。
。
根据您输入的日期,这可能会导致您的问题
One error I notice is that it can be possible to have 1 month and 0 weeks like if you were to enter April 15th. Which would result in only outputting the days and hours. Change the following.
to
This could be causing your problem depending on the date you entered.