Last active
August 29, 2015 14:22
-
-
Save antoniovassell/fed29e782be33b7eb5ab to your computer and use it in GitHub Desktop.
Override of dateTime input to display datetime values in the user's timezone. Note how I include my custom class to override the Form helper class
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::uses('Controller', 'Controller'); | |
App::uses('Category', 'Model'); | |
App::uses('CakeTime', 'Utility'); | |
/** | |
* Application Controller | |
* | |
* @package app.Controller | |
*/ | |
class AppController extends Controller { | |
/** | |
* Helpers | |
* | |
* NOTE how I include the form helper here | |
* | |
* @var array | |
*/ | |
public $helpers = array('Session', 'Time', 'Form' => array('className' => 'Template')); | |
/** | |
* before filter callback | |
* | |
* @return void | |
*/ | |
public function beforeFilter() { | |
Configure::write('Config.timezone', 'America/Jamaica'); | |
} | |
} |
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::uses('FormHelper', 'View/Helper'); | |
App::uses('CakeTime', 'Utility'); | |
/** | |
* Class TemplateHelper | |
* | |
* NOTE It extends the form helper | |
*/ | |
class TemplateHelper extends FormHelper { | |
/** | |
* Date time input function override that allows date time inputs to display | |
* the time in a user's timezone. We may need to add options to make this optional. | |
* | |
* @param string $fieldName | |
* @param string $dateFormat | |
* @param string $timeFormat | |
* @param array $attributes | |
* @return string | |
*/ | |
public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) { | |
$attributes = parent::value($attributes, $fieldName); | |
if (!empty($attributes['value'])) { | |
$attributes['value'] = CakeTime::format('Y-m-d H:i:s', $attributes['value']); | |
} | |
return parent::dateTime($fieldName, $dateFormat, $timeFormat, $attributes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment