I'm trying to show a list of our client's various time zones and their local time that is derived from that. Right now in my Client model I have the following function (doAfterFind) that is just a wrapper around afterFind that gives a consistent format.
public function doAfterFind($result = array()) {
$result = parent::doAfterFind($result);
if (!empty($result['timezone'])) {
App::uses('CakeTime', 'Utility');
$result['local_time'] = CakeTime::format(
VIEW_DATE_FORMAT,
strtotime('now'),
null,
$result['timezone']
);
}
return $result;
}
The local_time
field is correctly placed in the model after the find(). I have tried to implement this as a Virtual Field as well because it will need to be sorted. The problem is I can't figure out how to calculate the time based off the current time and client's timezone. I have tried the same in the model's constructor and adding it to the virtualFields array, but the time zone is not available during the constructor call - since there is no ID loaded or anything.
Does anyone know a way to either have MySQL calculate this, or use what I already have but as a virtual field compatible with Paginator sort()? I could format the date display in the View, but need the local time column to be sortable.
Notes:
My time zones are stored as the locale name (America/New_York, etc...).
The constant VIEW_DATE_FORMAT is just the date() format string F jS, Y - g:i A
.