Last active
August 29, 2015 14:12
-
-
Save chrisvogt/2407596369ec7d1310e3 to your computer and use it in GitHub Desktop.
Prettify find() datetime results in CakePHP using CakeTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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