I have the following function that converts a timestamp from YYYY-MM-DD HH:MM:SS to a nicer format like an hour ago or last week etc.
Example:
////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTION TO TRANSLATE DATE AND TIMES INTO NICE DATE EG 2 HOURS AGO ETC...
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
At the moment, the smallest time it will give is one hor ago. How can I change it so that it displays now if in the last few minutes or 15 minutes if within 15 minutes etc?
No, I added the fallbacks so if there isnât a date set it will return âno date providedâ and if the date has been set by default (ie a bunch of zeros) it displays bad date - but then they are hidden in with PHP on the page template if they return either of those values.
I think the bulk of the function came from posts on the W3 and StackOverflow if I rember correctly.
Is it a time zone issue, or a daylight savings time thing? For example, is the database time GMT and the current time BST which is an hour later, so in fact itâs correct because the difference is 1h4m.
No, I checked that out. The timestamp reads 18:10 - thats when i logged in to the account - I checked whether the conversion to ânice timeâ had happened at 18:14 and then again at 18:30.
Checking it again now - at 18:48 - the nice time now displays 2 hours ago - it should say 48 minutes ago. Definately shouldnât be saying 2 hours.
It seems to be starting at 1 hour when the login is made rather than a minute.
I can show you a screenshot of it working in a PHP test, though I think the function is poorly constructed and difficult to work in lacking documentation. This was just a quick fix.
The function lacks any documentation on the type of date the parameter should be. I had to look it up all up myself.
Am I wrong or is your first length index of â60â unnecessary because the timestamp result is already in seconds. Does the for loop not unnecessarily divide by seconds when it is already seconds? Just trying to understand how that for loop logic works.
Iâm not sure what the loop logic is doing but it is starting at an hour and seems to counting from there. So, effectively I think it is skipping the first hour.
So here is what I am getting. The function converts a timestamp into a unix time, and uses that only for all calculations. With that in mind I am working on establishing some consistency here.
Here is what I am getting using the following values:
Now: 1497898883 // time()
Date: 2017-06-19 01:00:00 // Static Timestamp
Time: 1497848400 // strtotime() of Date
Nice(): 14 minutes ago // Result of nicetime()
Difference: 50483 // Now > Time ? Now - Time : Time - Now
// Skip Diff Seconds (Unix is in seconds above)
Diff Minutes: 841.38333333333 // Difference / 60 (Because 60 seconds in minute)
Hours: 14.023055555556 // (Difference / 60) / 60 (Because 60 minutes in hour)
Days: 1.168587962963 // ((Difference 60) / 60) / 12) (Because 12 hours in a day)
Now following the pattern above, here is what the loop does
$lengths = array("60","60","24","7","4.35","12","10");
$difference = 50483;
// for each index of $lengths as $index while $difference > $index
$difference / 60;
$difference = 841.383333333;
$difference / 60;
$difference = 14.023055556;
// Because 14 is less than 24 (next index to divide by) stop here
// Returns "14 minutes ago."
Iâm not. This was all done by hand using the logic in the function. See my next comment using the for loop logic applied to my manual calculations above.
I carried the difference I determined between the two UNIX times above and brought it into the functionâs loop.
So my point in all this is to say, that determining the period by if the UNIX difference (in seconds) is greater than the next divisible length is not a way to determine itsâ period.
What you want to get to is a point where my initial fix makes sense. If you are calculating only in seconds, you can simply determine whether $difference is less than 15 minutes which would be the result of (60*15).
I would suggest making a separate function that determines time periods, and use that in this function. For example if you are always going to be calculating in UNIX seconds.
I think I understand the logic behind what youâve done - well the maths of it at least - not sure how I would rework to functiuon to work as I had originally intended.
$date = new DateTime("10pm");
$now = new DateTime("now");
echo "<br/><br/>It is ".$now->diff($date)->format("%h hours and %i minutes")." till then<br/><br/>.";
// It is 1 hours and 15 minutes till then
Might be able to combine the function with this determing if a time needs plural or not.