How can i convert a date like this: 2012-07-16 01:00:00 +00 (it's in the UTC +00:00 timezone) to UTC +04:00 timezone? Ensuring that daylight saving will be handelled correctly?
Use DateTime and DateTimeZone.
$date = new DateTime('2012-07-16 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // +04
echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00
To help with the solution, you need to get the last part of the string (the offset part) and look it up against a simple lookup. you can use a regex or substr() (maybe) to get the offest part. Then, when you have a + or - value, use a maximum of 24 lookups against possible timezones which you can use with PHP's possible timezones - if the offset is the same, who cares what the actual country/location is?
The use date_default_timezone_set to apply the right one.
The DateTimeZone constructor can explicitly accept a UTC offset, which I understand you have.
//So For 'Asia/Kolkata' you need to use
$timeZone = new DateTimeZone('+0530');
// Example
$utc = new DateTime("1960-08-08T20:40:00Z");
echo $utc->format('Y-m-d H:i:s T')."<br>";
$tz =new DateTimeZone('+0530');
$utc->setTimezone($tz);
echo $utc->format('Y-m-d H:i:s T');
Output:
1960-08-08 20:40:00 Z
1960-08-09 02:10:00 GMT+0530
I guess this is easiest and simplest way to convert time to any given Offset
Documentation:
http://php.net/manual/en/datetimezone.construct.php
Note: per that documentation link, this new constructor usage has been available since PHP version 5.5.10.
You can also use GMT time also and convert it to your requirement afterwards
<?php
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>
GMT refers Greenwich Mean Time which is common all over the world.
Related
I have a date in this format "Wed, 26 Oct 2022 16:11:30 -1100", need to convert it to UTC time and in a format so it can be inserted into a MySQL database.
The date was pulled from an email using "$headerInfo->date;". I don't see any way to receive the date any different.
Every way I try to do the conversion is painful and brute force.
Is there an elegant, or not painful way to do this?
TIA.
Been trying regex but it doesn't handle converting the month into digits, then you have the UTC offset (time zone) to work with.
Parse the date/time string using DateTimeImmutable::createFromFormat, set the timezone to UTC then format it to MySQL's datetime literal syntax
$dt = DateTimeImmutable::createFromFormat('D, j M Y H:i:s O', $dateString);
$mysqlFormat = $dt->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:sP');
Demo ~ https://3v4l.org/G7RBG
<?php
//just use strtotime
echo date('Y-m-d H:i:s e',strtotime('Wed, 26 Oct 2022 16:11:30 -1100'));
?>
This is the coding and it is echoing with the right format but the fact that the date is wrong when it prints out.
Output: 31 Dec 1969 19:33
Database Timestamp 2016-05-20 21:53:17
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i',$row['timestamp']);
?>
and i have tried doing the date in different ways and still the same result
In the code sample you posted, $row['timestamp'] has not been set, so the date is constructed with timestamp 0, also known as epoch, or the date that is being echoed.
If you change it as follows, it should work fine:
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i', $timestamp); ?>
Side note:
Time zone ECT is not a valid time zone code in PHP. If I assume correctly that you mean european central time, you would have to specify CET instead.
ECT doesn't exist as a valid TimeZone, did you mean CET perhaps?
The correct way to do this is using the DateTime class, i.e.:
$date = new DateTime();
$date->setTimestamp(1456778973);
$tz = new DateTimeZone("America/Denver");
$date->setTimezone($tz);
echo $date->format('d M Y H:i');
PHPFiddle Demo
Note:
Dates should always be stored in DB as UTC (timestamp aka unix time), then you can add or subtract the timezone offset using the DateTime class.
Would you know what the offset would be for mountain standard time?
Mountain Time: America/Denver
Mountain Time (no DST): America/Phoenix
List of Supported Timezones
I've an application,say the end user might be from any country but when he does some action,i want the date to be shown in a particular time zone.
I want insert this into DB so i'm doing this using date_default_timezone_set(''); with date()
Is this the right way or should i use gmdate() and add time zone.
Thanks
No, don't use date_default_timezone_set() for timezone conversions. This can have unintended side-effects.
Instead, use this:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimeZone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
Note, you're creating the DateTime object using UTC time and then applying the timezone. This way is much cleaner.
If you want GMT, I think you should use the right Timezone as Europe/London
date_default_timezone_set('Europe/London');
For example, I have a date time in this format and its UTC offset is +3:
2011-02-23 05:00:00
So its UTC time is actually:
2011-02-23 02:00:00
But what is the simplest way to do this conversion in PHP?
I have thought of using the DateTimeZone class but as you can see, I only have the offset but not the timezone name. And to construct a DateTimeZone object, the timezone name is required.
Maybe there are some better ways without using DateTimeZone class.
Thanks in advance.
Could just modify the date based on the offset. If you are interested in changing this to other timezones then create the date with the UTC timezone.
$date = new DateTime('2011-02-23 05:00:00', new DateTimeZone('UTC'));
$date->modify(sprintf('%s%d hours', $offset < 0 ? '+' : '-', $offset));
Note: This will work even if the offset is a string. i.e. "-3" or "+3"
Then for example if you wanted to see that time in Melbourne, Australia.
$date->setTimezone(new DateTimeZone('Australia/Melbourne'));
echo $date->format('c');
You can use date() and strtotime() for this.
$in = "2011-02-23 05:00:00";
$offset = "-3 hours";
$out = date('Y-m-d H:i:s',strtotime("$in $offset"));
It's also good to note that with this, you aren't restricted on what you're offsetting. The strtotime() function lets you do all kinds of wonderful things, like "-2 weeks +3 days ...", etc.
You are also free to get any date format from date(), or just use strtotime() alone to get the timestamp.
I have a timezone of the user(he chooses it from a list)
I have a time in UTC(not current time)
So I need something like GetTimeForRegion(time, timezone) for PHP. Is there such functions or libraries or services?
you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :
$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04
date('r') or date('c') may help you.
echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200
echo date('c') prints 2011-02-16T16:01:07+02:00
You need to look at the Date/Time API in PHP. I strongly advise you to stay away of gmdate and older date functions in php.
In your case, you should ask the user for its Olson based time zone.
The code of Artefact2 will do the trick.
please write this instead :
$date = date("Y-m-d H:i:s" , time());
so Y it means year m means month d means day
H get hours from 0 - 24
h get hours from 0 to 12
i get minutes
s get seconds