One of the constant issues when working with legacy data is how ugly it is. When you’re calling a stored procedure to pull out legacy data from an Oracle Storage Management System backend, the resulting text string is formatted in a way that… Well, let’s just say its hideous looking, or at least we can say that it’s not human readable. Fortunately I’ve come up with a large library of conversion methods to take this data and format it into a way that will look good in the application front end UI. For example here’s a here’s a sample from my library that can take a MySQL timestamp and format it to look nice on a web page or application front end. To save critical UI space and help differentiate old records from new ones, the display shows a different format for how old the record is. For example records from today will simply say what time they were entered. Records from this week will have the day of the week and records older than that will have the dates. I use techniques like these frequently while handling the data conversion from where it’s stored in a legacy back in to being displayed on the modern front end user interfaces.
//******************************************************************************** function formatDateFromDb($timestamp){ $time_object = new DateTime($timestamp, new DateTimeZone('UTC')); $time_object->setTimezone(new DateTimeZone('America/Los_Angeles')); /*echo("<p>records from last year {$time_object->format('m/d/y g:i A')} </p>"); echo("<p>records from last week {$time_object->format('D F j g:i A')} </p>"); echo("<p>records from current week (but not from today) {$time_object->format('D g:i A')} </p>"); echo("<p>records from current day {$time_object->format('g:i A')} </p>");*/ $current_year = date('Y'); $timestampyear = date('Y', strtotime($timestamp)); $current_week = date('W'); $timestampweek = date('W', strtotime($timestamp)); $current_day = date('N'); $timestampday = date('N', strtotime($timestamp)); $current_hour = date('H'); $timestamphour = date('H', strtotime($timestamp)); $prettyDate = ""; if($current_year - $timestampyear > 0) { //records from last year | 03/08/21 3:41 PM $prettyDate = $time_object->format('m/d/y g:i A'); } else { if($current_week - $timestampweek > 0){ //records from last week | Mon March 8 3:41 PM $prettyDate = $time_object->format('D F j g:i A'); } else if(($current_week - $timestampweek == 0 && $current_day - $timestampday >0) || ($current_week - $timestampweek == 0 && $current_day - $timestampday == 0 && $timestamphour < 8)){ // records from current week (but not from today) | Mon 3:41 PM $prettyDate = $time_object->format('D g:i A'); } else if($current_week - $timestampweek == 0 && $current_day - $timestampday == 0 && $timestamphour >= 8){ // records from current day | 3:41 PM $prettyDate = $time_object->format('g:i A'); } else { $prettyDate = "time format not found for {$timestamp}"; } } return $prettyDate; } //----------------------------------------------------- //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^