php 1个小时前,计算小时前在php中使用服务器时间,没有其他日期

我需要在没有任何其他日期或MySQL日期的情况下使用php中的服务器时间来计算小时数

服务器时间屏幕截图

而且我只是困惑自己如何将服务器时间转换为这一小时(6小时前)

这是我尝试自己时的尝试,但不确定是否正确.

echo $diff = date("H",abs(strtotime(date("Y-m-d h:i:s a")) - strtotime(date("Y-m-d 0:0:0 a"))));

解决方法:

试试这个代码:)

function timePassed($str) {

$sec = time() - strtotime($str);

if($sec < 60) {

/* if less than a minute, return seconds */

return $sec . " seconds ago";

}

else if($sec < 60*60) {

/* if less than an hour, return minutes */

return intval($sec / 60) . " minutes ago";

}

else if($sec < 24*60*60) {

/* if less than a day, return hours */

return intval($sec / 60 / 60) . " hours ago";

}

else {

/* else returns days */

return intval($sec / 60 / 60 / 24) . " days ago";

}

}

/* print: 8 hours ago */

echo timePassed("2012-03-27 05:36:25 am");

?>

标签:php,date,time

来源: https://codeday.me/bug/20191010/1886394.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值