Writing a PHP function that converts UK date to relative time

What would be the best way of converting UK date and time in the format:

30/01/2013 13:30:06

which is d/m/YH:i:s in PHP noation to relative time (ie just now, few minutes ago, 30 minutes ago, 3 hours ago, 1 day ago.. and so on). I've seen several tutorials on the subject but they all revolve around creating functions without any clear explanations. I would appreciate some assistance on the matter.


希望这可以帮助

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>';
            }
        }
    }

Writing a custom function might help. Cut the string and convert to numbers. Use mktime() to create a timestamp, compare it to time() current timestamp and switch (case) through various relative time possibilities.


Use mktime(); and date();

For example if you have a specific time in the format d/m/YH:i:s , and you want it as dmY H:s:i , you would explode the time into chunks using explode(); and then use date(new_format, mktime(current_format_chunks))

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

上一篇: 用PHP计算传递的时间

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