generate functionSun, 25 Dec 2022

"March 27, 2018" format to time ago

function get_timeago($ptime) { $estimate_time = time() - $ptime; if( $estimate_time < 1 ) { return 'less than 1 second ago'; } $condition = array( 12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second' ); foreach( $condition as $secs => $str ) { $d = $estimate_time / $secs; if( $d >= 1 ) { $r = round( $d ); return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago'; } } }

Questions about programming?Chat with your personal AI assistant