简体   繁体   English

在PHP中将时间戳转换为时间,例如1天前,2天前...

[英]Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…

In this already existing Q a found this : 在这个已经存在的Q中发现了这个:

$time_elapsed = timeAgo($time_ago); //The argument $time_ago is in timestamp (Y-m-d H:i:s)format.

//Function definition

function timeAgo($time_ago)
{
    $time_ago = strtotime($time_ago);
    $cur_time   = time();
    $time_elapsed   = $cur_time - $time_ago;
    $seconds    = $time_elapsed ;
    $minutes    = round($time_elapsed / 60 );
    $hours      = round($time_elapsed / 3600);
    $days       = round($time_elapsed / 86400 );
    $weeks      = round($time_elapsed / 604800);
    $months     = round($time_elapsed / 2600640 );
    $years      = round($time_elapsed / 31207680 );
    // Seconds
    if($seconds <= 60){
        return "just now";
    }
    //Minutes
    else if($minutes <=60){
        if($minutes==1){
            return "one minute ago";
        }
        else{
            return "$minutes minutes ago";
        }
    }
    //Hours
    else if($hours <=24){
        if($hours==1){
            return "an hour ago";
        }else{
            return "$hours hrs ago";
        }
    }
    //Days
    else if($days <= 7){
        if($days==1){
            return "yesterday";
        }else{
            return "$days days ago";
        }
    }
    //Weeks
    else if($weeks <= 4.3){
        if($weeks==1){
            return "a week ago";
        }else{
            return "$weeks weeks ago";
        }
    }
    //Months
    else if($months <=12){
        if($months==1){
            return "a month ago";
        }else{
            return "$months months ago";
        }
    }
    //Years
    else{
        if($years==1){
            return "one year ago";
        }else{
            return "$years years ago";
        }
    }
}

But how i write to this my number in this formatting ? 但是我如何在这种格式中写下我的号码? please help. 请帮忙。 (Sorry for this but this filter dont allow me to post this big code so please ignore this info about nothing only for posting my question ok ignore this thing in brackets I am not engish so sorry. will it work now ?) (对不起,但这个过滤器不允许我发布这个大代码,所以请忽略这个信息,只有发布我的问题好吧忽略括号中的这个东西我不是很遗憾所以对不起。现在可以吗?)

You give the function any string of time that strtotime() accepts: http://php.net/manual/en/function.strtotime.php 你给函数strtotime()接受的任何字符串时间: http//php.net/manual/en/function.strtotime.php

ie $time_ago_string = timeAgo('Jan 14th, 2007'); $time_ago_string = timeAgo('Jan 14th, 2007');

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

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