How to convert string into timezone aware date - php

Hello I have a string 2020-08-19 13:04:53. I know that this time is in Sydney, Australia time. However, I need to convert this time into New York, America time as a date object. How can I do this?
date_default_timezone_set("Australia/Sydney");
$time = '2020-08-19 13:04:53';
$date = date('Y-m-d H:i:s', $time);

I would try something like
<?php
$date = new DateTime('2020-08-19 13:04:53', new DateTimeZone('Australia/Sydney'));
echo $date->format('Y-m-d H:i:s');
// Output: 2020-08-19 13:04:53
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
// Output: 2020-08-18 23:04:53

Related

How to get current date and time with respect to Time Zone in Codeigniter

When I run the following code in codeigniter, it returns incorrect time and date for the India location. Please let me the solution.
'date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s', time());
echo $date;'
try this
$now = new DateTime();
$now->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $now->format('Y-m-d H:i:s');

PHP convert mktime result to UTC

I am using this code to generate yesterday's beginning of day in PST (aka America/Los_Angeles) time. I can't figure out how to convert the result to UTC.
date_default_timezone_set("America/Los_Angeles");
$time1 = date("Y-m-d H:i:s", mktime(0,0,0, date('n'), date('j')-1, date('Y')));
I tried this, but $time1 is not datetime, it's string. So the following won't work.
$time1->setTimezone(new DateTimeZone("UTC"));
The DateTime class can do all that for you
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // will use now
echo $date->format('d/m/Y H:i:s'); //16/08/2016 16:13:29
$date->setTime(0,0,0);
$date->modify('-1 day');
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 00:00:00
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 07:00:00

How to change timezones and format time in PHP?

I have a variable like this,
$Timestamp = "Tue Mar 8 15:59:00 UTC-05:00 2016";
How do I change its format to YYYYMM-DD HH:MM AM/PM and change the timezone from UTC to Pacific Time using PHP?
PHP's DateTime object is pretty flexible.
$UTC = new DateTimeZone("UTC");
$TZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2016-03-09 15:00:00", $UTC );
$date->setTimezone( $TZ );
echo $date->format('Y-m-d H:i:s');
Use php date: date('Y/m/d H:i', $timestamp) but your timestamp should be int:
$timestamp = strtotime('22-09-2008');
You can try following
$Timestamp = "Tue Mar 1 15:59:00 UTC-05:00 2016";
$datetime = new DateTime($Timestamp);
$datetime->format('Y-m-d H:i:s') . "\n";
$new_time = new DateTimeZone('Pacific/Apia');
$datetime->setTimezone($new_time);
//New formatted time
echo $datetime->format('Y-m-d H:i:s');

Convert the example date format to required format

I ma getting a response from a Logistic partner like 2013-03-28T13:15:00+05:30 I want this in IST format date. How do i convert it properly.
$addTime = explode('+',$response->TrackDetails->ActualDeliveryTimestamp);
if($addTime[1] == '05:30'){
$time = '';
$time = strtotime('+5:30 hrs',$addTime[0]);
echo date('Y-m-d H:i:s',strtotime($addTime[0]));
}
I tried the above code. but does not work properly.
$addTime = explode('+','2013-03-28T13:15:00+05:30');
list($hours, $minutes) = explode(':', $addTime[1]);
$dt = new DateTime($addTime[0]);
$dt->add(new DateInterval('PT'.intval($hours).'H'));
$dt->add(new DateInterval('PT'.intval($minutes).'M'));
echo $dt->format('Y-m-d H:i:s');
See it in action
EDIT
This is more accurate although the results isn't what you'd expect because of daylight savings time:
$dt = new DateTime('2013-03-28T13:15:00+05:30');
$dt->setTimeZone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s');
See it in action

how to convert php date formats to GMT and vice versa?

i am new to php.
i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.
Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.
Here's code to switch to GMT
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));
and back to the default timezone...
$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.
e.g.
// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');
// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');
// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));
// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');
// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.
Explicit example of converting from a timezone to GMT
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using your Asia/Kolkata to America/New_York
date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Use the gmdate function to convert to GMT time.
For example
$d = '2011-03-28 12:05:20';
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
// Convert local time to gmt
public function convertTime($timezone,$time){
$selectedtime = date("Y-m-d H:i",strtotime($time));
$date = new DateTime($selectedtime, new DateTimeZone($timezone));
$date->setTimezone(new DateTimeZone('GMT'));
$convertedtime = strtotime($date->format('Y-m-d H:i'));
return $convertedtime;
}

Categories