Showing yesterday, today, tomorrow when showing the current date

Hello,

When displaying an existing date in a table, how to show it like “Today 12:12:00” or “Yesterday 10:15:00” or “Tomorrow 15:30:00” instead of “2023-10-14 12:12:00”?
What is the logic of this?

Wouldn’t you write a function, accepting the datetime value as an input, then get the current date, the current date -1, and the current date +1, then compare the date part of the input value to those three dates and if it matches, change the date part to the corresponding strings ‘Today’, ‘Yesterday’, or ‘Tomorrow’?

Which would look like -

<?php

// example date
$date = '2023-10-14 12:12:00';

function near_date($date)
{
	// calculate the 'near' dates
	$y_d = date('Y-m-d', strtotime('-1 day'));
	$c_d = date('Y-m-d');
	$t_d = date('Y-m-d', strtotime('+1 day'));
	
	// build a map array for the 'near' dates
	$map[$y_d] = 'Yesterday';
	$map[$c_d] = 'Today';
	$map[$t_d] = 'Tomorrow';
	
	// break the input date into date and time parts
	list($d,$t) = explode(' ',$date);
	
	if(!isset($map[$d]))
	{
		// not one of the 'near' dates, return the original value
		return $date;
	}
	
	// is one of the 'near' dates, return the 'near' date value
	return "$map[$d] $t";
}

echo near_date($date);
1 Like

Thank you very much.

I searched online for how to do this but couldn’t find anything.
I also learned how to use list() explode() and it was very useful.

It works very well as a normal date, no problems.
date_tr() function is for displaying local language
Is it possible to use the following date display format with the code above?

date_tr('d M Y, l, H:i', 1698181200); => 25 Eki 2023, Çarşamba, 00:00

Yes.

It appears that the date_tr() function expects a unix timestamp. For the case of not return the yesterday, today, or tomorrow format, you would use strtotime() on the $date value to produce a unix timestamp, to supply to the date_tr() call. Hopefully, the date_tr() function was written properly, to return the formatted value, rather than outputting it directly.

For the case of returning the yesterday, today, or tomorrow format, you are apparently only using the hours and minutes (the H:i format.) You would need to modify the $t value in my code to remove the seconds part.

1 Like

I made an edit as follows and the result is successful, there is no problem for now.

function near_date($unix_timestamp)
{
	$date = date_tr('Y-m-d H:i:s', $unix_timestamp);

	// calculate the 'near' dates
	$y_d = date('Y-m-d', strtotime('-1 day'));
	$c_d = date('Y-m-d');
	$t_d = date('Y-m-d', strtotime('+1 day'));
	
	// build a map array for the 'near' dates
	$map[$y_d] = 'Dün';
	$map[$c_d] = 'Bugün';
	$map[$t_d] = 'Yarın';
	
	// break the input date into date and time parts
	list($d,$t) = explode(' ',$date);
	
	if(!isset($map[$d]))
	{
		// not one of the 'near' dates, return the original value
		return date_tr('d M Y, l, H:i:s', $unix_timestamp);
	}
	
	// is one of the 'near' dates, return the 'near' date value
	return "$map[$d] $t";
}

echo near_date(1698181200);
Sponsor our Newsletter | Privacy Policy | Terms of Service