0

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...

hellosheikh
  • 2,929
  • 8
  • 49
  • 115

3 Answers3

2

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.

JorickL
  • 165
  • 10
  • thankyou ... i think this is the exact solution which i was needed ,, but only one problem is coming ... the error is coming saying that Call to a member function format() on a non-object .. i have enabled the helpers also in my controller – hellosheikh Aug 05 '13 at 17:25
  • Did you `echo` the helper? Like so: `Time->format($format, $date, $default = NULL, $timezone); ?>` And what does the stacktrace reports? – JorickL Aug 05 '13 at 20:57
  • I see you've found the solution... http://stackoverflow.com/questions/18065086/cakephp-fatal-error-call-to-a-member-function-format-on-a-non-object – JorickL Aug 06 '13 at 07:11
  • yeah i have found it .. as i was using your code into controller..which was the wrong approach as helpers are developed to be used in view .. – hellosheikh Aug 06 '13 at 10:57
0
<?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);
?>
emsoff
  • 1,585
  • 2
  • 11
  • 16
0
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
xLight
  • 91
  • 1
  • 5