I want to list recent articles on a home page, but would like to have the dates displayed as follows:
Recent Articles
|- Article 5 (posted
Today)
|- Article 4 (posted
Yesterday)
|- Article 3 (posted
Monday)
|- Article 2 (posted
Friday)
|- Article 1 (posted
2 wks ago)
So, it will need to compare the current system date/time to the article date source (according to &dateSource=``), and output:
- for articles posted today or yesterday -
’Today’ or
’Yesterday’
- for articles posted within the 7 days, excluding today and yesterday - the
weekday it was posted on, eg ’Monday’ or ’Friday’
- for articles older than one week, the
number of weeks/months that have passed - eg ’2 wks ago’ or ’3 months ago’
Would it be best to create a snippet or a PHX Custom modifier for this? For my purpose it would be used to formatting dates appearing in a Ditto list. Perhaps the PHX [+date:timesince+] modifier could be a good place to begin?
[+date:timesince+] (added by Apox)
<?php
if (!function_exists('timeSince'))
{
function timeSince($timestamp)
{
if ($timestamp >= time())
{
$timeago = '0 seconds ago.';
return $timeago;
}
$seconds = time()-$timestamp;
$units = array(
'year' => 31556926,
'month' => 2629743,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
// 'second' => 1
);
$timeago = '';
foreach ($units as $key => $val)
{
if ($seconds >= $val)
{
$results = floor($seconds/$val);
$seconds = ($seconds-($results*$val));
$timeago .= ($results >= 2) ? $results . ' ' . $key . 's, ' : $results . ' ' . $key . ', ';
}
}
return rtrim($timeago, ', ') . ' ago';
}
}
return timeSince($output);
?>