PHP datetime conversion giving me wrong time - php

I am converting 08/22/2015 10:56 PM CST to UTC
Conversions
10:56PM CST = 3:56 AM UTC
Therefore the date should be: 2015-08-23 03:56:00 +04
The code below is giving me: 2015-08-23 04:56:00 +04 (which is incorrect)
Code in Repl
https://repl.it/repls/LovelyKhakiExtension
$datetime = '08/22/2015 10:56 PM';
$tz_from = 'CST';
$tz_to = 'UTC';
$format = 'Y-m-d H:i:s +h';
$dt = new DateTime($datetime, new DateTimeZone($tz_from));
$dt->setTimeZone(new DateTimeZone($tz_to));
echo $dt->format($format) . "\n";

Related

Converting from UTC to any timezone

My linux box is set to use UTC. Given a timezone and a date, I want to get the date range so that I can query the database for records created on any given day. For instance, if it is now 2018-03-24 at 9am in America/Denver timezone. I want to get the start and end times for this date in UTC. How can I get the UTC equivilant of the beginning of that date?
<?php
$date = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone('America/Denver'));
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d 00:00:00');
?>
this returns 2018-03-24 00:00:00 which is not correct. Any pointers?
Try using this function.
function UTCTimeToLocalTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
$tz = date_default_timezone_get();
$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
DateTimeZone('UTC'));
$local_datetime = $utc_datetime;
$local_datetime->setTimeZone(new DateTimeZone($tz));
return $local_datetime->format($ToDateFormat);
}
echo UTCTimeToLocalTime('2015-07-01 13:30:00','America/Denver');
function LocalTimeToUTCTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
$tz = date_default_timezone_get();
$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
DateTimeZone($tz));
$local_datetime = $utc_datetime;
$local_datetime->setTimeZone(new DateTimeZone('UTC'));
return $local_datetime->format($ToDateFormat);
}
You feed the DateTime constructor with a bogus local time:
new DateTime(date('Y-m-d H:i:s'), newDateTimeZone('America/Denver'));
^^^^
You tell PHP that's a Denver local time but you don't really know. Since the string does not contain time zone information PHP will use the default time zone.
Just drop date(). It serves no purpose and only makes things harder.
<?php
$date = new DateTime('now', new DateTimeZone('America/Denver'));
echo $date->format('r'), PHP_EOL;
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('r'), PHP_EOL;
Sun, 25 Mar 2018 06:11:21 -0600
Sun, 25 Mar 2018 12:11:21 +0000

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');

PHP timezone issuse with Pacific/Auckland - Getting different value

$user_timezone_from = 'UTC';
$user_timezone_to = 'Pacific/Auckland';
$date = new DateTime('2015-08-21 14:00', new DateTimeZone($user_timezone_from));
$date->setTimezone(new DateTimeZone($user_timezone_to));
echo $cur_gmt_date = $date->format('Y-m-d H:i:s');
$date = new DateTime('2015-09-29 14:00', new DateTimeZone($user_timezone_from));
$date->setTimezone(new DateTimeZone($user_timezone_to));
echo $cur_gmt_date = $date->format('Y-m-d H:i:s');
I am getting different results for this.
2015-08-22 02:00:00
2015-09-30 03:00:00 It should be 2015-09-30 02:00:00
Why I got wrong value?
Wikipedia tells me that on the last Sunday of September there's a DST change in New Zealand. Hence a one hour difference to Summer Time.

How to Convert PST to GMT time in php?

I would like convert PST to GMT.
Ex:
PST : 22:00 need to convert in to GMT. I have to consider about DAY LIGHT SAVING TIME month also.
How can i do that?
Use PHP's built-in DateTime class...
Use DateTime objects which have this functionality built in:
$date = new DateTime('2013-08-06 15:00:00', new DateTimeZone('America/Los_Angeles'));
echo "The time in Los Angeles is " . $date->format('Y-m-d H:i:s') . "<br>";
$date->setTimezone(new DateTimeZone('Europe/London'));
echo "The time in London is " . $date->format('Y-m-d H:i:s') . "<br>";
(Example Code) (Full Documentation)
I have to do similar for one my work and here it is how approached this:
//date time in PST
$t = 'Tue Feb 04 23:02:09 PST 2014';
$dateTime = DateTime::createFromFormat("D M d H:i:s \P\S\T Y", $t, (new DateTimeZone('America/Los_Angeles')));
var_dump($dateTime); // date time in PST format
$dateTime->setTimezone(new DateTimeZone('UTC'));
var_dump($dateTime); // date time in GMT format

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