计算小时前使用服务器时间在php没有任何其他日期 [英] Calculate hours ago using server time in php without any other date

查看:103
本文介绍了计算小时前使用服务器时间在php没有任何其他日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用php中的服务器时间计算小时,而不需要任何其他或MySQL日期。

I need to calculate hours using Server time in php without any other or MySQL dates

服务器时间屏幕截图

我只是混淆自己如何将服务器时间转换为这个小时(6小时前)

and I just confuse myself how to convert server time to this hour (6 hours ago)

这是我尝试自己的尝试但是我不知道这是不是正确的方式。

Here is my attempt when I tried myself but I am not sure is this correct way or not.

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


推荐答案

尝试此代码:)

<?php
  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没有任何其他日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆