Krysti.Engineer Official Chat
You are viewing an inferior version of the site because your browser does not support WebP. Do upgrade to something like Chrome or Firefox. Loading websites like this causes them to require a fallback set of images and they are almost always lower quality and larger in size.

Human Readable Printing Of How Long Ago A UNIX Timestamp Was

The best way I can explain the application of this function is this is to benefit the USER when reading back a time needs to be absolutely clear to anybody reading in a very friendly format.
2022-07-18T:00:00:00+00:00 July 18 2022 00:00:00

<?php
function timeAgo($stamp,$chk = "") {
	if($chk==""){ $chk = time(); }
	if($stamp>$chk) { $diff = $stamp-$chk; } else { $diff = $chk-$stamp; }
	switch($diff) {
		case ($diff>31556926): { 
			$years = floor($diff/31556926);
			$ago = $years." year(s) ago";
			if($years==0){ $ago = "Now"; }	
			break;
		}
		case ($diff>2629743): { 
			$months = floor($diff/2629743);
			$ago = $months." month(s) ago";
			break; 
		}
		case ($diff>604800): { 
			$weeks = floor($diff/604800);
			$ago = $weeks." week(s) ago";
			break; 
		}
		case ($diff>86400): { 
			$days = floor($diff/86400);
			$ago = $days." day(s) ago";
			break;
		}
		case ($diff>3400): { 
			$hours = floor($diff/3400);
			$ago = $hours. " hour(s) ago";
			break; 
		}
		case ($diff>60): { 
			$minutes = floor($diff/60);
			$ago = $minutes. " minute(s) ago";
			break; 
		}
		case (($diff<60)&&($diff>1)): { 
			$ago = $diff. " second(s) ago"; 
			break; 
		}
		case ($diff<=1): {
			$ago = "Now";
			break;
		}
		case ($diff==0): { 
			$ago = "Now";
			break; 
		}
	}
	return $ago;
}
?>

This PHP function will allow you to print how long a timestamp was in a human readable format. You can also compare it against another past time by using the 2nd parameter provided. This is useful for presenting last login to the user, a comment system's posts, etc.

Contact Krysti

@itskrystibitch Twitter Photo
GitHub:
@itskrystibitch
IRC:
Official Chat
Direct.Me:
@Krysti
Ko-fi:
@Krysti
Photos:
Virtual Krysti
E-Mail / Notify of errors:
coder [@] krysti.engineer
Please be patient contacting me, I don't really check much of social media or anything. If you use the IRC be sure to stick around because that's how IRC works, silly. :P