php - Timezone helper for CakePHP -
i have built simple timezone helper cakephp app using various examples here on stack overflow saves users zone against user record. e.g. europe/london.
to see helper: http://pastebin.com/vgivwqhj
when user logs application, save date/time of when logged in can display list of logs user security reasons. e.g. $this->user->savefield('lastlogin', date('y-m-d h:i:s'));
however date pull server date/time. how can make use of timezone of user amend it?
i tried:
$date = new datetime(null, new datetimezone($user['user']['timezone']));
and then: $this->user->savefield('lastlogin', $date->date);
null...
but if debug $date example:
object(datetime) { date => '2013-04-01 15:22:32' timezone_type => (int) 3 timezone => 'europe/london' }
1.) doing wrong? how pull datetime object?
2.) way handle this? means have replace date()
calls snippet users time.
3.) won't dates on website become mismatched example if user in europe posts after user in usa because local time used display different, possibly though before usa counterpart.
should instead save server time , use snippet above display differently user, times same timezone displayed relative user?
i'm not sure can save datetime
object right savefield()
. must convert string in format cakephp believes universal:
$this->user->savefield('lastlogin', $date->format('y-m-d h:i:s'))
your original code triggering notice:
notice: undefined property: datetime::$date
... because there's no date
public property in datetime class.
about other questions, point out cannot compare local times belong different time zones, cannot compare kilometres miles. if store local times need store time zone , make conversion everytime. it's simpler normalise dates , store them same time zone (either utc or server's time zone) , perform conversion when displaying information. makes more sense because local time of post author irrelevant else.
Comments
Post a Comment