Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

timestamp

I am trying to convert a timestamp of the format 2009-09-12 20:57:19 and turn it into something like 3 minutes ago with PHP.

I found a useful script to do this, but I think it's looking for a different format to be used as the time variable. The script I'm wanting to modify to work with this format is:

function _ago($tm,$rcs = 0) {     $cur_tm = time();      $dif = $cur_tm-$tm;     $pds = array('second','minute','hour','day','week','month','year','decade');     $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);      for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);         $no = floor($no);         if($no <> 1)             $pds[$v] .='s';         $x = sprintf("%d %s ",$no,$pds[$v]);         if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0))             $x .= time_ago($_tm);         return $x;     } 

I think on those first few lines the script is trying to do something that looks like this (different date format math):

$dif = 1252809479 - 2009-09-12 20:57:19; 

How would I go about converting my timestamp into that (unix?) format?

like image 260
willdanceforfun Avatar asked Sep 13 '09 02:09

willdanceforfun


People also ask

How convert date from yyyy mm dd to dd mm yyyy format in PHP?

Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.

What does PHP time () return?

The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

How can I timestamp in PHP?

Definition and Usage. The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

Use example :

echo time_elapsed_string('2013-05-01 00:22:35'); echo time_elapsed_string('@1367367755'); # timestamp input echo time_elapsed_string('2013-05-01 00:22:35', true); 

Input can be any supported date and time format.

Output :

4 months ago 4 months ago 4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago 

Function :

function time_elapsed_string($datetime, $full = false) {     $now = new DateTime;     $ago = new DateTime($datetime);     $diff = $now->diff($ago);      $diff->w = floor($diff->d / 7);     $diff->d -= $diff->w * 7;      $string = array(         'y' => 'year',         'm' => 'month',         'w' => 'week',         'd' => 'day',         'h' => 'hour',         'i' => 'minute',         's' => 'second',     );     foreach ($string as $k => &$v) {         if ($diff->$k) {             $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');         } else {             unset($string[$k]);         }     }      if (!$full) $string = array_slice($string, 0, 1);     return $string ? implode(', ', $string) . ' ago' : 'just now'; } 
like image 174
Glavić Avatar answered Sep 30 '22 17:09

Glavić


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';         }     } } 
like image 33
Ayman Hussein Avatar answered Sep 30 '22 16:09

Ayman Hussein