编写一个将英国日期转换为相对时间的PHP函数

以英文格式转换英国日期和时间的最佳方式是什么?

30/01/2013 13:30:06

这是d / m / YH:i:在PHP记法中的相对时间(即现在,几分钟前,30分钟前,3小时前,1天前等等)。 我已经看到了关于这个主题的几个教程,但它们都是围绕创建功能而没有任何明确的解释。 我希望就此事提供一些协助。


希望这可以帮助

function timeSince($ptime){
        $etime = time() - strtotime($ptime);

        if( $etime < 1 ){
            return 'less than 1 second ago';
        }

        $a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
                    30 * 24 * 60 * 60       =>  'month',
                    24 * 60 * 60            =>  'day',
                    60 * 60             =>  'hour',
                    60                  =>  'minute',
                    1                   =>  'second'
        );

        foreach( $a as $secs => $str ){

            $d = $etime / $secs;     
            if( $d >= 1 ){

                $r = round( $d );
                return ' <font style="color:#0099ff"> ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago</font>';
            }
        }
    }

编写自定义函数可能会有帮助。 剪切字符串并转换为数字。 使用mktime()创建时间戳,通过各种相对时间可能性将它与time()当前时间戳和开关(大小写)进行比较。


使用mktime();date();

例如,如果你有一个格式为d/m/YH:i:s的特定时间,并且你希望它作为dmY H:s:i ,你可以使用explode();将时间分解为块explode(); 然后使用date(new_format, mktime(current_format_chunks))

链接地址: http://www.djcxy.com/p/10109.html

上一篇: Writing a PHP function that converts UK date to relative time

下一篇: What is the best way to store duration in MySQL