如何显示2个月前的价值

时间:2016-05-27 08:29:02

标签: php php-5.2

这里我正在写一个函数,从这段代码不能正常工作,$ joindate值1年完成意味着,它将显示01年前,但假设10个月只完成意味着我想要显示10个月前,同样的天,我认为如果条件我错了,这里

echo $since_start->format('%Y years %m Months %a days %h hours %i minuts %s seconds');//00 years 2 Months 61 days 13 hours 44 minuts 24 seconds.

我会得到正确的价值



function timeAgo($logintime)
{
date_default_timezone_set('UTC');
date_default_timezone_set('Asia/Kolkata');
$start_date = new DateTime($logintime);
$since_start = $start_date->diff(new DateTime(date("Y-m-d h:i:s")));
if( intval($since_start->format('%Y') ) >= 1){
    echo $year = $since_start->format('%Y years ago');
}
else if(intval($since_start->format('%m')) >= 12){
    echo $months = $since_start->format('%m month ago');
}
else if(intval($since_start->format('%a')) >= 1){
    echo $days = $since_start->format('%a days ago');
}
else if(intval($since_start->format('%h')) >= 1){
    echo $hourss = $since_start->format('%h hours ago');    
}
else if(intval($since_start->format('%i')) >= 1){
    echo $min = $since_start->format('%i minuts ago');  
}
else if(intval($since_start->format('%s')) >= 1){
    echo $min = $since_start->format('%s seconds ago'); 
}
    
}




1 个答案:

答案 0 :(得分:0)

Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...

function time_elapsed_string($ptime)

{
$etime = time() - $ptime;

if ($etime < 1)
{
    return '0 seconds';
}

$a = array( 365 * 24 * 60 * 60  =>  'year',
             30 * 24 * 60 * 60  =>  'month',
                  24 * 60 * 60  =>  'day',
                       60 * 60  =>  'hour',
                            60  =>  'minute',
                             1  =>  'second'
            );
$a_plural = array( 'year'   => 'years',
                   'month'  => 'months',
                   'day'    => 'days',
                   'hour'   => 'hours',
                   'minute' => 'minutes',
                   'second' => 'seconds'
            );

foreach ($a as $secs => $str)
{
    $d = $etime / $secs;
    if ($d >= 1)
    {
        $r = round($d);
        return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
    }
  }
}

或使用您的代码,但您可以修改它

        <?php
function joining_date($joiningdate){
    date_default_timezone_set('UTC');
    date_default_timezone_set('Asia/Kolkata');
    $start_date = new DateTime($joiningdate);//$joiningdate =2016-03-27
    $since_start = $start_date->diff(new DateTime(date("Y-m-d H:i:s")));
    echo $since_start->format('%Y years %m Months %a days %h hours %i minuts %s seconds');//00 years 2 Months 61 days 13 hours 44 minuts 24 seconds

    if( intval($since_start->format('%Y') ) >= 1){
        echo $year = $since_start->format('%Y years ago');
    }
    else if(intval($since_start->format('%m')) >= 1){
        echo $months = $since_start->format('%m month ago');
    }
    else if(intval($since_start->format('%a')) >= 1){
        echo $days = $since_start->format('%a days ago');
    }
    else if(intval($since_start->format('%g')) >= 1){
        echo $hourss = $since_start->format('%g hours ago');    
    }
    else if(intval($since_start->format('%i')) >= 1){
        echo $min = $since_start->format('%i minuts ago');  
    }
    else if(intval($since_start->format('%s')) >= 1){
        echo $min = $since_start->format('%s seconds ago'); 
    }
    }
    joining_date('2016-05-27 02:40:00');
?>