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


i trying convert timestamp of format 2009-09-12 20:57:19 , turn 3 minutes ago php.

i found useful script this, think it's looking different format used time variable. script i'm wanting modify work 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 first few lines script trying looks (different date format math):

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

how go converting timestamp (unix?) format?

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 supported date , time format.

output :

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

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -