Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 377 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing timezone of a retrieved date in php

#1
I am retrieving a date in format of 2013-09-15 08:45:00 from the database, which is set in UTC and I need to change it to another dynamic timezone (based on user)

So far I've got

$datetime = $row->field_data_field_performance_times_field_performance_times_v;
$eventDate = DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone($user->timezone));
$performance_time = date_format($eventDate, 'l, j F, Y, H:i');

But it doesn't change the timezone. Any ideas what's wrong? It should be +2 hours in my case.
Reply

#2
Your input datetime is in UTC, not user's timezone. So first you must create datetime object in UTC, and then set/change timezone to user's :

<!-- language: php -->

$dt = new DateTime('2013-09-15 08:45:00', new DateTimeZone('UTC'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 08:45:00
[timezone_type] => 3
[timezone] => UTC
)
*/


Now you have datetime in UTC timezone. If you wish to change timezone, just call `->setTimezone()` on DateTime object :

<!-- language: php -->

$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 10:45:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
*/

p.s. because input `2013-09-15 08:45:00` is in standard datetime format, you don't need to use `DateTime::createFromFormat`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through