-1

I am picking one dateTime from mysql database i want difference of these from now.

php function date_diff is suitable to making difference but i want output like following Actually i want to show last lime of login in system.

first date: 2013-12-07 12:26:10
Another date: Today

Output should be:

20 min
or
1 hour
or 
2 hour
or 
6 hour
or 
Yesterday
or
2 days ago

i have no idea how to achieve this. any help would be appriciated.

Thanks in advance

Krish R
  • 22,583
  • 7
  • 50
  • 59
Manwal
  • 23,450
  • 12
  • 63
  • 93
  • Take a look here: http://it2.php.net/manual/en/datetime.diff.php – ProGM Dec 09 '13 at 06:38
  • hopefully this would help you http://stackoverflow.com/questions/10681752/human-readable-date-reference-in-php-e-g-last-monday – Mujtaba Haider Dec 09 '13 at 06:42
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Glavić Dec 09 '13 at 09:54

3 Answers3

1

Hi below is a function which convert time like 2013-12-07 12:26:10 to human readable time. You may edit this to achieve your requirements

<?php
function ToHumanReadable($timestamp){
    $difference = time() - strtotime($timestamp);
    $periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");

    if ($difference > 0) { // this was in the past
        $ending = "ago";
    } else { // this was in the future
        $difference = -$difference;
        $ending = "to go";
    }       
    for($j = 0; $difference >= $lengths[$j]; $j++) $difference /= $lengths[$j];
    $difference = round($difference);
    if($difference != 1) $periods[$j].= "s";
    $text = $difference." ".$periods[$j]." ".$ending;
    return $text;
}
echo ToHumanReadable('2013-12-07 12:26:10'); 
?>

http://www.weberdev.com/get_example.php3?ExampleID=4769

Mujtaba Haider
  • 1,650
  • 2
  • 19
  • 29
  • when i pass 2013-12-09 13:03:41 then it returning 2 hours to go. why ?? this is not correct. – Manwal Dec 09 '13 at 07:47
  • why not correct? Are you running this on local? Or your server may be somewhere in the west where this time still not happened – Mujtaba Haider Dec 09 '13 at 08:54
1

From PHP Version > 5 below new date/time functions added to get difference:

$datetime1 = new DateTime("2010-06-20");
$datetime2 = new DateTime("2011-06-22");
$difference = $datetime1->diff($datetime2);

echo 'Difference: '.$difference->y.' years, ' 
                   .$difference->m.' months, ' 
                   .$difference->d.' days';

print_r($difference);

Result as below:

Difference: 1 years, 0 months, 2 days
DateInterval Object
(
    [y] => 1
    [m] => 0
    [d] => 2
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 367
)
Vidhi
  • 2,026
  • 2
  • 20
  • 31
0

check this

 $date1 = strtotime('2013-12-07 12:26:10');
 $date2 = time();

 $diff = abs($date2 - $date1);
 $years = floor($diff / (365*60*60*24));
 $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
 $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
 die(var_dump($days));
ruba
  • 147
  • 1
  • 2
  • 7