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.
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 :
$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 :
$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.
Related
I use the native PHP DateTime class for adding days to dates. But when dealing with negative dates, I encountered a strange bug. Depending on the millennium added or a day or two. Example:
$date_one = date_create("-1000-12-27");
date_modify($date_one, '+1 day');
//Return DateTime Object ( [date] => -1000-12-29 00:00:00 )
$date_two = date_create("-2000-12-27");
date_modify($date_two, '+1 day');
//Return DateTime Object ( [date] => -2000-12-28 00:00:00 )
$date_three = date_create("-3000-12-27");
date_modify($date_three, '+1 day');
//Return DateTime Object ( [date] => -3000-12-29 00:00:00 )
That is, depending on the parity of the millennium issue, or December 28 or December 29. Why is this happening? What is the problem?
When I create new DateTime object, it has timezone from "date.timezone" setting:
print_r(new DateTime());
DateTime Object
(
[date] => 2015-03-02 03:19:50.000000
[timezone_type] => 3
[timezone] => Europe/Moscow
)
But when I get DateTime object from MongoDate, it has UTC timezone:
print_r((new MongoDate()) -> toDateTime())
DateTime Object
(
[date] => 2015-03-02 00:19:50.000000
[timezone_type] => 1
[timezone] => +00:00
)
How can I setup MongoDate to create DateTime objects with my default timezone?
A way from do this:
$mongoDate = new \MongoDate();
$dateTimeDefaultTimeZone = $mongoDate->toDateTime()->setTimezone(new \DateTimeZone(date_default_timezone_get()))
You will can get the DateTime with default TimeZone.
I have a timestring comming from an XML File
1408226400
Now i want to convert this string into an DateTime object, but i'm getting the wrong result.
//This is an example for this Question, but with the real datestring
//My real code looks like this: $dbTurn->setStartDate(new \DateTime("#".$turn['cruiseStartDateString']));
$test = date("d-m-Y H:i", 1408226400);
$dateTime = new DateTime($test);
print_r($dateTime);
# result is DateTime Object ( [date] => 2014-08-16 18:00:00 [timezone_type] => 3 [timezone] => America/New_York )
But that is not the date i expected, because the result i should because is this:
2014-08-17 10:04:00
Also, taken from the real code i mentioned in the comment within the code, this snippet:
$dbTurn->setStartDate(new \DateTime($turn['cruiseStartDateString']));
Throws this error if i don't suppress the error message:
message => (string) DateTime::__construct(): Failed to parse time string (1408226400) at position 7 (4): Unexpected character
Is something wrong with the datestring or am i doing something wrong?
You don't need to create a formatted datestring using date() before passing to a DateTime object constructor:
$timestamp = 1408226400;
$dateTime = new DateTime('#' . $timestamp);
print_r($dateTime);
though you may also need to pass a timezone to the DateTime constructor as well
you can try this
date_default_timezone_set('Europe/Berlin');
$test = date("Y-m-d h:i:s", 1408226400);
$dateTime = new DateTime($test);
print_r($dateTime);
OUTPUT :
DateTime Object
(
[date] => 2014-08-17 12:00:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
When user enters Date and time using 'input type=date name="date"' && 'input type=time name="time"' tags according to his/her timezone,
for eg :- If a user from India, (Asia/Kolkata) timezone entered a date : 1/22/14 and time : 5:30pm , I need to Convert it into UTC time stamp for storing it to DB, To Achive that
I used below code:-
$year=substr($date,0,4);
$month=substr($date,5,2);
$day=substr($date,8);
$hour=substr($time,0,2);
$minute=substr($time,3);
$clientzone=mysql_result(mysql_query("select timezone from c_users_extra where c_id='{$_SESSION['clientid']}'"),0,0); //Fetches the timezone of user
date_default_timezone_set($clientzone);//Setting default timezone to client timezone
$datetime = new DateTime("$year-$month-$day $hour:$minute:00");
$la_time = new DateTimeZone('UTC');
$datetime->setTimezone($la_time);
$values=$datetime->format('Y-m-d H:i:s');
$year=substr($values,0,4);
$month=substr($values,5,2);
$hour=substr($values,11,2);
$minutes=substr($values,14,2);
$seconds=substr($values,17,2);
$timestamp=mktime($hour,$minutes,$seconds,$month,$day,$year);//creating new timestamp from coverted
print_r(getdate($timestamp));//Result : Array ( [seconds] => 0 [minutes] => 30 [hours] => 6 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 )
//Expected Result : Array ( [seconds] => 0 [minutes] => 0 [hours] => 12 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 )
Why I get this wrong time stamp ?
Here is an object oriented example:
<?php
$date = '2014-01-22 18:15:00'; // assumed date is formatted correctly
$clientzone = 'America/New_York'; // assumed timezone is a valid one from your SQL
$dateObj = new DateTime($date, new DateTimeZone($clientzone));
echo "Original: " . $dateObj->format('Y-m-d H:i:sP') . "\n";
$dateObj->setTimezone(new DateTimeZone('UTC')); // convert to UTC
echo "Converted: " . $dateObj->format('Y-m-d H:i:sP') . "\n";
echo "Epoch: ".$dateObj->format('U');
?>
You can format that just like the date function.
The $date and $clientzone are assumed to be valid.
Can't you do something simpler like:
$user_time = strtotime($date);
$dateTimeZone = new DateTimeZone($timezone);
// get the offset from server time (UTC)
$tzOffset = $dateTimeZone->getOffset(new DateTime());
$result_time = $user_time - $tzOffset;
Try following function to convert Local date time to UTC date time
function LocaltoUTC($date_to_convert)
{
$local_timestamp = strtotime($date_t);
$UTC_timestamp += ((-(5.5)) * 3600); // 5.5 is UTC timezone or local timezone
$gmt_datetime = gmdate('y-m-d H:i:s', $UTC_timestamp);
return $gmt_datetime;
}
Here is a similar question. Take a look at Adjusting time zone in PHP with DateTime / DateTimeZone
There is a solution and a more comfortable way to do your task.
I have the following date string
$date="Sat Apr 30 2011 18:47:47 GMT+0900 (Tokyo)"
I want to convert it to UTC time
$timestamp_UNIX = strtotime($date);
echo date("Y-m-d\TH:i:s\Z",$timestamp_UNIX);
Why do I got
2011-04-30T11:47:47Z
and not
2011-04-30T09:47:47Z
The problem is that you code does not automatically echo UTC. It echos the timestamp in whatever your default timezone is set to. This is done via date_default_timezone_set() at runtime or via the configuration setting date.timezone in your php.ini.
The modern way would be to use the DateTime and the DateTimeZone classes.
$d = new DateTime('Sat Apr 30 2011 18:47:47 GMT+0900 (Tokyo)');
print_r($d);
$d->setTimezone(new DateTimeZone('UTC'));
print_r($d);
prints
DateTime Object
(
[date] => 2011-04-30 18:47:47
[timezone_type] => 1
[timezone] => +09:00
)
DateTime Object
(
[date] => 2011-04-30 09:47:47
[timezone_type] => 3
[timezone] => UTC
)
You should use gmdate() instead of date() (or you could check the DateTime and DateTimeZone classes in PHP 5.2 / 5.3)