Hi all
I have a function which shows the dates and strings for future events, works very well. My problem is, any dates past 31st december 2014 shows as 340 day ago, instead of the date 1st January 2015 and so on… it seems to be referencing the past year of 2014 instead of the future year of 2015, haven’t teated 2016 or 2017 though I’m sure it will be the same.
Can anybody see what is causing this issue and how I can amend the function to print the correct string for any dates after 31st december 2014?
function dateString(DateTime $dt, $switch = false) {
$dt->modify('midnight');
$now = new DateTime('midnight');
$diff = $now->diff($dt);
$days = $diff->days;
if ($now > $dt) {
$days *= -1;
}
switch (true) {
case $days === 0:
return 'Today';
case $days === 1:
return 'Tomorrow';
case $days === -1:
return 'Yesterday';
case $days < -1:
if(!$switch)
return abs($days).' days ago';
case $days > 1:
if($switch)
return $days.' days';
default:
return ($switch) ? abs($days).' days ago' : $dt->format('l, j F');
} }
Thanks, Barry