Reputation: 2566
Hey everyone im trying to display relative date and time messages to the user, so for example if the file was updated less then 5 seconds ago the message the user will see is: 'Updated Just Now' however it doesnt seem to be working.
Here is the code that I am using
function relativeTime($date) {
$timeNow = date('M d Y H:s');
if ($date == $timeNow || $date == date('M d Y H:s', + 500)) {
echo "Updated Just Now";
} else {
echo "not now";
}
}
This is where I am displaying it and passing it the argument:
<?php relativeTime(dateUTCtoLocal("M d Y H:s", $arrSchool["updated"]));?>
I eventually will have a few if's in there for minute and hours, am I going about this the right way?
Thanks in advance.
Upvotes: 0
Views: 210
Reputation: 76666
You can convert the input date string into a Unix timestamp using strtotime()
and calculate the difference between the input date and current time, and display the message if the difference is less than 5 minutes (5 * 60 = 300 seconds):
function relativeTime($date) {
$timeNow = date('M d Y H:s');
$diff = strtotime($timeNow) - strtotime($date);
if ($diff < (5*60)) {
echo "Updated Just Now";
} else {
echo "not now";
}
}
Upvotes: 2
Reputation: 324820
Basically you're looking for if the time it was posted is less than five seconds ago. Easy, but you MUST use timestamps. Formatted times are no good.
relativeTime($arrSchool['updated']); // if it's a numeric timestamp
relativeTime(strtotime($arrSchool['updated'])); // if it's a datetime string
function relativeTime($timestamp) {
if( $timestamp > time()-5) {
echo "Updated Just Now";
}
else {
echo "not now";
}
}
Upvotes: 0