Relative Dates code written 5 years ago
If you’re a hardcore sennir reader, you might remember me being very proud of my dodgy relative dates code I wrote over a year ago. It turns out that code was broken, and has been ever since I wrote it.
This is where you come in. Well Hopefully.
<?php
/*
* Takes a datetime, and returns a nice string, saying
* datetime of the form "YYYY-MM-DD HH:MM:SS"
* 2 days ago, 10 minutes ago 1 hour ago, 3 weeks ago, 4 months ago, 6 years ago.
* If "$to" is provided, then we should return the time "until" or "from"
* that time. Using the style "1 day later" or "3 hours ago".
*/
function datetimeToRelative($datetime, $to=0)
{
if($to === 0)/* Get the current time + DST */
$current_time = time();
else
$current_time = strtotime($to);
/* Get the time of the post */
$post_time = strtotime($datetime);
/* Get when yesterday was */
$yesterday_time = strtotime('12am yesterday');
/* Work out the difference between the two dates; should be positive if in past */
$relative_time = $current_time - $post_time;
$abs_relative_time = abs($relative_time);
$when = ($relative_time > 0 ? 'ago' : 'later');
if ($abs_relative_time < 60) // Less than a minute
return $abs_relative_time.' '.($abs_relative_time > 1 ? 'seconds' : 'second').' '.$when;
elseif ($abs_relative_time < 3600) // Less than an hour
return ($minutes = floor($abs_relative_time / 60)).' '.($minutes > 1 ? 'minutes' : 'minute').' '.$when;
elseif ($post_time > $yesterday_time && $post_time < $yesterday_time + 86400) // Was it yesterday ?
{
if($relative_time > 0)
return 'yesterday';
else
return '1 day '.$when;
}
elseif ($abs_relative_time < 86400) // Less than a day ago
return ($hours = floor($abs_relative_time / 3600)).' '.($hours > 1 ? 'hours' : 'hour').' '.$when;
// echo "abs relative time is:" .$abs_relative_time;
/* Since we have ruled out hours / mins / secs, now forget about the times etc. */
$post_time = strtotime(substr($datetime, 0, 10).' 00:00:00');
$relative_time = $current_time - $post_time;
$abs_relative_time = abs($relative_time);
// echo "now abs relative time is:" .$abs_relative_time;
if ($abs_relative_time < 604800) // Less than a week
return ($days = floor($abs_relative_time / 86400)).' '.($days > 1 ? 'days' : 'day').' '.$when;
elseif ($abs_relative_time < 2629743) // Less than an month
return ($weeks = floor($abs_relative_time / 604800)).' '.($weeks > 1 ? 'weeks' : 'week').' '.$when;
elseif ($abs_relative_time < 31556926) // Less than an year
return ($months = floor($abs_relative_time / 2629743)).' '.($months > 1 ? 'months' : 'month').' '.$when;
else
return ($years = floor($abs_relative_time / 31556926)).' '.($years > 1 ? 'years' : 'year').' '.$when;
}
?>
There’s the code.. can anybody fix it?
In the meantime, anywhere a relative date is used, it’ll just be the datetime stamp.
There’s cookies on offer!
Update
Looks like there was only a small error in the code, and so, hopefully, it’s fixed. If any other weirdiosities occur, please let me know!
There are no responses to this entry
Do you want to say something? Use the form below to add your thoughts.
Note if you submit your comments, they'll appear here! yes, right below this. Try and make them relevant eh?
Leave a reply
Notes on leaving replies:
Anything you type here will be visible to the world! type wisely.
Want to make your comment look pretty? Use markdown syntax for advanced formatting and link options.
Got Gravatar? Stick your email address in!
By using this, you agree to the Privacy Policy.
Site navigation