Posted in PHP & MySQL

PHP – How to Print “ZY Time Ago” from Date and Time in different language

PHP – How to Print “ZY Time Ago” from Date and Time in different language Posted on September 8, 20201 Comment
I'm Hari Prasad Chaudhary from Nepal, developer of the finest educational website/app "MeroSpark" and the finest eCommerce system "PasalaY". Here I share the code snippet which I learn during development.

In this post, I have given two functions that are used to convert date and time to “time ago” on English or any other language you want to show on PHP language. The date and time will be converted to “Year-month-day-hour-minute-second ago” or just now. The functions are given below.

Function to show “Time ago” with PHP

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

You can use this function like below:

$date = "2016-10-11";
$time = "11:12:11 AM";
echo time_passed($date.' '.$time);
//output: 3 years ago

//passing true on full string display
echo time_passed($date.' '.$time, true);
//output: 3 years, 10 months, 4 weeks, 2 hours, 30 minutes, 51 seconds ago

Print “Time ago” in different languages.

To print “Time ago” on different language, replace “Year”,”Month”,”Day”,”Hour”,”Minute”,”Second”,”Just Now”, “ago” with your own linguistic terms. And to convert numbers to any other language, use the following example. The following function is for Devnagrik numbers.

function nepnum($raw){
	$number = "";
	for($x = 0; $x < strlen($raw); $x++){
		$char = substr($raw, $x, 1);
		if($char == '1'){
			$number = $number."१";
		}elseif($char == '2'){
			$number = $number."२";
		}elseif($char == '3'){
			$number = $number."३";
		}elseif($char == '4'){
			$number = $number."४";
		}elseif($char == '5'){
			$number = $number."५";
		}elseif($char == '6'){
			$number = $number."६";
		}elseif($char == '7'){
			$number = $number."७";
		}elseif($char == '8'){
			$number = $number."८";
		}elseif($char == '9'){
			$number = $number."९";
		}elseif($char == '0'){
			$number = $number."०";
		}else{
			$number = $number.$char;
		}
	}
	return $number;
}

Use this function like below:

$date = "2016-10-11";
$time = "11:12:11 AM";

echo nepnum("4254789");
//output: ४२५४७८९

echo nepnum(time_passed($date.' '.$time));
//output: ३ years ago

echo nepnum(time_passed($date.' '.$time, true));
//output: ३ years, १० months, ४ weeks, २ hours, ३६ minutes, ४७ seconds ago

In this way, you can show “Time ago” in different languages with PHP script on your program using the above functions.

I'm Hari Prasad Chaudhary from Nepal, developer of the finest educational website/app "MeroSpark" and the finest eCommerce system "PasalaY". Here I share the code snippet which I learn during development.

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *