Due dates as dates in osTicket

So, I noticed that osTicket prefers to show dates for due dates and certain other fields as “human readable” dates. It displays something like Due in 2 days instead of 3/2/21 14:30 which was what I preferred; so they were the same as all the other dates. Sorting on these fields was often unreliable and the various phrases broke table displays and generally drove my OCD bonkers. Unfortunately, there is no way to change how these dates were displayed in the settings. So, I had to dig into the code.

The first thing I did was search the PHP code base for the phrases and found them all located in include/class.format.php about line 1056 in osTicket 15.1. I commented out the entire relativeTime($to, $from=false, $granularity=1) method and replaced it with my own.

function relativeTime($to, $from = false, $granularity = 1)
{
    global $cfg;

    if (!$to) return false;

    // Create a date object in the timezone of the items in the database, which is UTC.
    $date = date_create(date(DATE_ISO8601, $to), timezone_open('UTC'));

    // Change to the local timezone, which displays the date/time of the object in the local zone w/ DST also.
    $date->setTimezone(timezone_open($cfg->getDefaultTimezone()));

    return $date->format('m/d/y G:i');
}

So I’ll briefly unpack what’s going on here. First, we need to access the configuration to get the time zone (and we probably could get the time format, too.) Then we check the time (in this case the $to variable) to see if it’s got a value. If not, we return false which leaves the table block blank.

If $to has a value we create date instance with the UTC time zone. The reason is because osTicket stores dates in the database in UTC. This allows us to smoothly convert the time to the local time zone later in the next step using the setTimezone() method and injecting the time zone found in the $cfg variable.

All that’s left is to format the date and kick it back.

Everything was working fine until…I noticed that periodically the times reverted back to their wordy counterparts in the front end. As we all know JavaScript is the language of the front end. So I started poking around and lo and behold found the file located at scp/js/scp.js contained code that periodically adjusted relative times…at least that what the comment said! I just commented out that entire function and had to flush my browser’s cache to pick up the updated file. Once I did all that the constant transformation stopped and my happy actual date/times lived unfettered by constant JS transformations.

Sorting is now easy and reliable and, at least to me, things are more readable and understandable.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.