Reputation: 3015
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
5.00
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...
Upvotes: 0
Views: 2138
Reputation: 101
echo (new DateTime('2012-07-16 01:00:00 UTC'))
-> setTimezone(new DateTimeZone('GMT+8') )
->format('Y-m-d H:i:s');
will ouput
2012-07-16 09:00:00
Upvotes: 0
Reputation: 165
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.
Upvotes: 2
Reputation: 1605
<?php
$utc = gmdate("M d Y H:i:s"); // place your gmt timestamp here
echo $utc."<br>";
$offset = date('Z'); //gets offset from gmt
echo $offset."<br>";
$localtime = strtotime($utc) + $offset; // adjusts to localtime.
echo date("M d Y H:i:s", $localtime);
?>
Upvotes: 0