dongyong1897 2012-03-27 11:34
浏览 14
已采纳

计算小时前使用php中的服务器时间,没有任何其他日期

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

Server time Screen shot

server time

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"))));
  • 写回答

2条回答 默认 最新

  • douyuan9512 2012-03-27 11:48
    关注

    Try this code :)

    <?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");
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?