【问题标题】:Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…在 PHP 中将时间戳转换为时间前,例如 1 天前、2 天前……
【发布时间】:2017-10-21 08:22:43
【问题描述】:

在这个已经存在的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";
        }
    }
}

但是我如何以这种格式写入我的号码?请帮忙。 (很抱歉,但这个过滤器不允许我发布这个大代码,所以请忽略这个信息,只是为了发布我的问题,好吧,忽略括号中的这个东西,我不是很抱歉。它现在可以工作吗?)

【问题讨论】:

  • 反转输出顺序 - 从年到秒

标签: php


【解决方案1】:

你给函数strtotime()接受的任何时间字符串:http://php.net/manual/en/function.strtotime.php

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

【讨论】:

  • 我得到了 Epoch 时间,所以有什么可以将这些东西结合在一起的吗?
  • 我认为您只需注释掉第一条语句(strtotime 行)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
  • 2016-03-04
  • 1970-01-01
相关资源
最近更新 更多