Skip to content

Instantly share code, notes, and snippets.

@chrisvogt
Last active August 29, 2015 14:12
Show Gist options
  • Save chrisvogt/2407596369ec7d1310e3 to your computer and use it in GitHub Desktop.
Save chrisvogt/2407596369ec7d1310e3 to your computer and use it in GitHub Desktop.
Prettify find() datetime results in CakePHP using CakeTime
<?php # /app/Model/AppModel.php
/**
* afterFind() override
*
* @param mixed $results
* @param boolean $primary
* @return mixed
*/
public function afterFind($results, $primary = false) {
parent::afterFind($results, $primary);
return $this->_prettifyDates($results);
}
/**
* Reformat date fields
*
* @param mixed $results
* @uses CakeTime()
* @return mixed
*/
protected function _prettifyDates($results) {
$datetime_fields = array('created', 'modified');
foreach ($datetime_fields as $needle) {
foreach ($results as $key => $val) {
if (isset($val[key($val)][$needle])) {
$results[$key][key($val)][$needle] = CakeTime::niceShort($val[key($val)][$needle]);
}
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment