kasthuri S kasthuri S - 6 months ago 15
PHP Question

how to showing the value for 2 months ago

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



<?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($Y <= 1){
echo $year = $since_start->format('%Y years ago');
}
else if($m <= 12){
echo $months = $since_start->format('%m years ago');//not working
}
else if($h <= 24){
echo $days = $since_start->format('%d years ago'); //not working
}
}
?>




Answer

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')) >= 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('%h minuts ago');  
    }
    else if(intval($since_start->format('%s')) >= 1){
        echo $min = $since_start->format('%h seconds ago'); 
    }
    }
    joining_date('2016-05-27 02:40:00');
?>