简体   繁体   English

自函数错误以来的PHP时间

[英]PHP Time Since Function Bug

i am writing a time since function to return the time since a given mysql datetime. 我正在编写一个自函数起的时间,以返回自给定mysql datetime以来的时间。 When taking the $oldtime from current time() it is returning a negative int when i need a positive int. 当从当前time()中获取$ oldtime时,当我需要一个正整数时,它返回一个负整数。 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');

it could be much more convenient if you use PHP's DateTime and DateInterval classes and their methods: 如果使用PHP的DateTimeDateInterval类及其方法,可能会更加方便:

function timeSince($datetime) {
    $now        = strtotime("now");
    $then       = strtotime($datetime);
    $dt_now     = new DateTime("@" . $now);
    $dt_then    = new DateTime("@" . $then);

    //DateTime's diff method returns a DateInterval object which got a format method:
    return $dt_now->diff($dt_then)->format('%a days, %h hours, %i minutes and %s seconds');
}


some test cases: 一些测试用例:

//my local date & time is around "2016-02-25 19:49:00" when testing
echo '<pre>';

echo timeSince('2016-02-25 19:30:00');
//0 days, 0 hours, 19 minutes and 11 seconds
echo PHP_EOL;

echo timeSince('2013-11-02 15:43:12'); 
//845 days, 4 hours, 4 minutes and 3 seconds
echo PHP_EOL;

echo timeSince('2017-01-31 00:22:45'); 
//340 days, 4 hours, 35 minutes and 30 seconds
echo PHP_EOL;

echo timeSince('1950-05-14 07:10:05');
//24028 days, 12 hours, 37 minutes and 10 seconds
echo PHP_EOL;


code partially based on this answer: https://stackoverflow.com/a/19680778/3391783 代码部分基于此答案: https : //stackoverflow.com/a/19680778/3391783

strtotime uses timezone in your PHP settings. strtotime在PHP设置中使用时区。 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 . 例如,在我的乌克兰服务器上, strtotime('2016-02-25 14:35:00') 1456403700 strtotime('2016-02-25 14:35:00')转换为1456403700 ,在另一个时区(美国/太平洋)的服务器上,其转换为1456439700

Quote from PHP docs: 引用PHP文档:

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. 该函数期望得到一个包含英语日期格式的字符串,并将尝试将该格式解析为相对于现在给出的时间戳的Unix时间戳(自1970年1月1日00:00:00 UTC以来的秒数),或者当前时间(如果未提供)。

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. 有关定义默认时区的各种方法,请参见date_default_timezone_get()。

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

In your example, $oldtime must be smaller value than current time() . 在您的示例中, $oldtime必须小于当前time()值。

So, if you want to count time between larger value, simply reverse your equation: 因此,如果要在更大的值之间计算时间,只需逆转方程式即可:

This line: $time = time() - $oldtime; 这行代码: $time = time() - $oldtime;

Becomes: $time = $oldtime - time(); 变为: $time = $oldtime - time();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM