PHP 自函数错误以来的时间 [英] PHP Time Since Function Bug

查看:46
本文介绍了PHP 自函数错误以来的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 time since 函数来返回自给定 mysql datetime 以来的时间.从当前 time() 获取 $oldtime 时,当我需要一个正整数时,它返回一个负整数.我以前用其他语言编写过类似的函数,但我对这个问题视而不见,因此非常感谢任何帮助.

i am writing a time since function to return the time since a given mysql datetime. When taking the $oldtime from current time() it is returning a negative int when i need a positive int. I have written similar functions before in other languages but i have become blind to this problem, so any help would be much appreciated.

function timeSince($time){
        $today = date("Y");
        $oldtime = strtotime($time);
        $time = time() - $oldtime;
        $tokens = array (
            3600 => 'h',
            60 => 'm',
            1 => 's'
        );

        if($time >= 86400){
        }
    }

echo timeSince('2016-02-25 14:35:00');

推荐答案

strtotime 在 PHP 设置中使用时区.根据设置的时区,它可能会转换为尚未发生的时间.例如,在我的乌克兰服务器上,strtotime('2016-02-25 14:35:00') 转换为 1456403700,在另一个时区(美国/Pacific) 转换为 1456439700.

strtotime uses timezone in your PHP settings. Depending on timezone set, it might convert to the time that is yet to happen. For example, on my ukrainian server, strtotime('2016-02-25 14:35:00') converts to 1456403700, on a server in another timezone (US/Pacific) it converts to 1456439700.

引用自 PHP 文档:

Quote from PHP docs:

该函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数),相对于中给出的时间戳现在,如果没有提供现在,则为当前时间.

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

该函数的每个参数都使用默认时区,除非在该参数中指定了时区.除非有意,否则请注意不要在每个参数中使用不同的时区.有关定义默认时区的各种方法,请参阅 date_default_timezone_get().

Each parameter of this function uses the default time zone unless a time zone is specified in that parameter. Be careful not to use different time zones in each parameter unless that is intended. See date_default_timezone_get() on the various ways to define the default time zone.

您可以将 UTC/GMT 偏移量添加到您的日期时间(第一个参数),例如 strtotime('2016-02-25 14:35:00 +0800')('2016-02-25 14:35:00 GMT+08:00') 将转换为 1456382100

You can add UTC/GMT offset to your datetime (1st param), for example strtotime('2016-02-25 14:35:00 +0800') or ('2016-02-25 14:35:00 GMT+08:00') will convert to 1456382100

这篇关于PHP 自函数错误以来的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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