hellosheikh hellosheikh - 8 months ago 30
PHP Question

datetime timezone converting cakephp / php

i am working on a Cakephp 2.x .. well the problem which i have has nothing to do this with cakephp .. so the scenario is i have a page called settings in which user sets his timezone according to his country and in database i am storing the time in gmt format in Userinfo table
so in userinfo table the gmt time is


now in my other tables i have a field called datetime in which i am storing the datetime in this format

2013-06-14 10:28:00

now on my view pages i want to display the data with dateTime of particular user ...

what i want is i want to add the gmt to this datetime so i can get the final datetime according to the user country ... hope you undertstand what i want to say...


Use Cake's build-in Timehelper. Add the Helper in your controller public $helpers = array('Time'); and then in your view:

$this->Time->format($format = NULL, $date, $default = false, $timezone = NULL)

In your case that'll be: $this->Time->format('d-m-Y H:i', $data['data']['datetime'], NULL, $data['User']['timezone']);

Good luck with that. :)

NOTE: Since CakePHP 2.2 the order of $format and $date is changed. The way I explained is for CakePHP 2.2 or higher.