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.
Related
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 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');
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');
I am sure the answer is right in front of me, but I have a UTC dateTime that looks like this:
2014-01-02 04:02:58
All I want to do is add some hours to that.
How can I achieve this in PHP? Further, how would I add days to it if I wanted to?
Use DateTime(). Unlike date()/strtotime() it is timezone and daylight savings time friendly.
// PHP 5.2+
$dt = new DateTime('2014-01-02 04:02:58');
$dt->modify('+2 hours');
echo $dt->format('Y-m-d H:i:s');
$dt->modify('+2 days');
echo $dt->format('Y-m-d H:i:s');
See it in action
Or
// PHP 5.3+
$dt = new DateTime('2014-01-02 04:02:58');
$dt->add(new DateInterval('PT2H'));
echo $dt->format('Y-m-d H:i:s');
$dt->add(new DateInterval('P2D'));
echo $dt->format('Y-m-d H:i:s');
See it in action
Or
// PHP 5.4+
echo (new DateTime('2014-01-02 04:02:58'))->add(new DateInterval('PT2H'))->format('Y-m-d H:i:s');
See it in action
Reference:
DateTime()
DateInterval()
You can use strtotime() try like this :
echo $new_time = date("Y-m-d H:i:s", strtotime( "2014-01-02 04:02:58".'+3 hours'));
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.