-1

Here i am writing one function,from this code not working properly,$joiningdate value 1 year finished means,it will showing 01 years ago,but suppose 10 months only finished means i want to show 10 months ago,same for days,i think that if condition my be wrong,here

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.

i will get correct value

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'); 
}
    
}
kasthuri S
  • 39
  • 1
  • 7
  • 1
    You have alot of if's but none of those variables are defined?! Also, might want to change the text in every if instead of year every where? – Naruto May 27 '16 at 08:32
  • As @Naruto commented your variables are out of the scope of function like as you've only defined `$joiningdate` and your variables `$Y,$m,$h` are out of the scope of the function – Narendrasingh Sisodia May 27 '16 at 08:40

1 Answers1

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

or with your code but you sould modify it

        <?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');
?>
Community
  • 1
  • 1
B.Kevin
  • 250
  • 3
  • 12