Skip to content

Instantly share code, notes, and snippets.

@antoniovassell
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoniovassell/fed29e782be33b7eb5ab to your computer and use it in GitHub Desktop.
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
<?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');
}
}
<?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