I want to convert the UTC datetime to any other time zone.its working fine.
But it showing only for one time zone,so to view the other time zone need to change the code every time.
i want to convert the UTC datetime to any other time zone dynamically.may be by selecting drop down or some other way.
<?php
$date = new DateTime('2017-11-15 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Asia/Calcutta'));
echo $date->format('Y-m-d H:i:s');
?>
You can timezone by this code where you can change the $timezone variable value from your dropdown.
$timezone= 'Asia/Calcutta';
$date = new DateTime('2017-11-15 01:00:00 +00');
$date = new DateTime($date, new DateTimeZone($timezone));
echo $date->format('Y-m-d H:i:s');
Related
I want to convert date UTC for Europe/Lisbon, but the code I have gives me different outputs/times:
$datafull = "13-04-2021 08:47:13";
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
echo $date->format('d-m-Y H:i:s (e)');
// 13-04-2021 09:47:13 (Europe/Lisbon)
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
print $datetime->format('d-m-Y H:i:s (e)');
// 13-04-2021 08:47:13 (Europe/Lisbon)
When you supply a timezone object to the DateTime constructor you're telling it in what timezone the give $datafull is. So in:
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
You say it is in Europe/Lisbon, and it stays there.
In the other code:
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
The default timezone is used when the DateTime is constructed, probably UTC on your server, and then you change it afterwards on the second line to Europe/Lisbon, which is an hour ahead.
See: DateTime::__construct
In php how can I display the current date and time with the timezone offset such as this:
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s");
//
This displays 2019-01-09 19:30:19
but I want it to display 2019-01-09 07:00:00 because I am in Mountain Time Zone
When working with time zones and DateTime() objects you should be using DateTimeZone() instead of date_default_timezone_set()
$now = new \DateTime(null, new DateTimeZone('America/Denver'));
echo $now->format("Y-m-d h:i:s");
Demo
Try to paste second parameter your timezone
$date = new DateTime('NOW', new DateTimeZone('America/Denver));
echo $date->format('Y-m-d h:i:s');
You could define wich timezone is fitting for you by this answer.
List of US Time Zones for PHP to use?
for getting all timezones names in you country. Then you pick your timezone and paste
$date = new DateTime('NOW', new DateTimeZone('Your Country/Your timezone'));
you have right code, but you have want to show time in 12 hours format. then you have use something like that.
date_default_timezone_set('America/Denver');
$now = new \DateTime('NOW');
echo $now->format("Y-m-d h:i:s A");
I am trying to convert time between current time to UTC and UTC to current time zone.
Here is what I have done:
$schedule_date = new DateTime($triggerOn, new DateTimeZone('UTC') );
$triggerOn = $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn;
The output value does not change the only thing that changes in format.
the string $triggerOn was generated based on America/Los_Angeles timezone
This is how my string looks like before and after:
BEFORE 04/01/2013 03:08 PM
AFTER 2013-04-01 15:08:00
So the issue here is that DateTime does not convert to UTC.
What you're looking for is this:
$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';
echo $triggerOn; // echoes 04/01/2013 03:08 PM
$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn = $schedule_date->format('Y-m-d H:i:s');
echo $triggerOn; // echoes 2013-04-01 22:08:00
You are consuming the date/time and setting the time zone correctly, however before formatting the datetime, you are not setting the desired output timezone. Here is an example which accepts a UTC time zone, and converts the date/time to the America/Los_Angeles time zone:
<?php
$original_datetime = '04/01/2013 03:08 PM';
$original_timezone = new DateTimeZone('UTC');
// Instantiate the DateTime object, setting it's date, time and time zone.
$datetime = new DateTime($original_datetime, $original_timezone);
// Set the DateTime object's time zone to convert the time appropriately.
$target_timezone = new DateTimeZone('America/Los_Angeles');
$datetime->setTimeZone($target_timezone);
// Outputs a date/time string based on the time zone you've set on the object.
$triggerOn = $datetime->format('Y-m-d H:i:s');
// Print the date/time string.
print $triggerOn; // 2013-04-01 08:08:00
Create the date using the local timezone, then call DateTime::setTimeZone() to change it.
i set time zone in php:
date_default_timezone_set("Europe/Istanbul");
ini_set('date.timezone', 'Europe/Istanbul');
then when i get the date time and time zone
date('Y-m-d H:i'); date_default_timezone_get();
that prints
2012-01-10 05:20 Europe/Istanbul
time zone is correct but time is wrong,
any ideas, what would be the reason?
thanks!
If your PHP is >= 5.2.0 then you can use the DateTime object:
$my_timezone = new DateTimeZone('America/Los_Angeles');
$far_away_timezone = new DateTimeZone('Europe/Istanbul');
$dt = new DateTime(date('Y-m-d H:i:s'), $my_timezone);
$dt->setTimezone($far_away_timezone);
echo $dt->format('Y-m-d H:i');
Hope it helps.
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;
}