Convert date time to utc php
I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries. Show asked Jan 19, 2010 at 17:55
0
Use strtotime to generate a timestamp from the given string (interpreted as local time) and use gmdate to get it as a formatted UTC date back. ExampleAs requested, here’s a simple example:
answered Jan 19, 2010 at 18:06
pokepoke 348k66 gold badges534 silver badges580 bronze badges 12 Using DateTime:
answered Dec 12, 2014 at 7:24
joerxjoerx 1,9781 gold badge15 silver badges18 bronze badges 2 Try the getTimezone and setTimezone, see the example (But this does use a Class) UPDATE: Without any classes you could try something like this:
NOTE: You might need to set the timezone back to the original as well
answered Jan 19, 2010 at 18:08
Phill PaffordPhill Pafford 81.7k90 gold badges260 silver badges380 bronze badges 7 Do this way:
or simply
to get "NOW" in UTC. Check the reference: http://www.php.net/manual/en/function.gmdate.php
laurent 85k73 gold badges271 silver badges412 bronze badges answered Jul 3, 2011 at 5:32
AttilioAttilio 2,0912 gold badges21 silver badges20 bronze badges 0 If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.
This will print this:
And as you can see it takes care of the daylight savings time problem as well. A little strange way to solve it.... :) answered Feb 14, 2010 at 21:50
JohanJohan 19.4k28 gold badges91 silver badges110 bronze badges 3 Convert local time zone string to UTC string.
https://en.wikipedia.org/wiki/Time_in_New_Zealand answered May 4, 2016 at 5:32
Frank HouFrank Hou 1,6281 gold badge15 silver badges11 bronze badges 0 If you don't mind using PHP's DateTime class, which has been available since PHP 5.2.0, then there are several scenarios that might fit your situation:
answered Dec 17, 2018 at 10:00
As strtotime requires specific input format, DateTime::createFromFormat could be used (php 5.3+ is required)
answered Aug 18, 2012 at 19:17
I sometime use this method:
Works all MOST of the time. answered Apr 16, 2012 at 14:46
aorcsikaorcsik 14.8k4 gold badges38 silver badges49 bronze badges 1 answered Jan 19, 2010 at 18:06
psychotikpsychotik 37k34 gold badges99 silver badges135 bronze badges With PHP 5 or superior, you may use datetime::format function (see documentation http://us.php.net/manual/en/datetime.format.php)
answered Aug 9, 2012 at 11:05
MUY BelgiumMUY Belgium 2,2124 gold badges30 silver badges43 bronze badges try
will output:
you should change format time to see other output answered Oct 18, 2011 at 16:42
General purpose normalisation function to format any timestamp from any timezone to other. Very useful for storing datetimestamps of users from different timezones in a relational database. For database comparisons store timestamp as UTC and use with
Usage:
answered Dec 9, 2016 at 5:26
SandeepSandeep 27.1k3 gold badges31 silver badges23 bronze badges As an improvement on Phill Pafford's answer (I did not understand his 'Y-d-mTG:i:sz' and he suggested to revert timezone). So I propose this (I complicated by changing the HMTL format in plain/text...):
answered Sep 11, 2017 at 17:31
2 alternatively you can try this:
this will output :
you can use this inside the value attribute of a text input box if you only want to display a read-only date. remove the 'e' if you do not wish to show your region/country. answered Oct 25, 2017 at 9:16
Bruce TongBruce Tong 1,25813 silver badges13 bronze badges Follow these steps to get UTC time of any timezone set in user's local system (This will be required for web applications to save different timezones to UTC):
answered Nov 9, 2018 at 10:16
Pradeep KumarPradeep Kumar 3,8952 gold badges33 silver badges39 bronze badges How can I get UTC in PHP?Use gmdate() to Get UTC Time
The gmdate() function will format a date and time in UTC. Once you supply gmdate() with your date time format, e.g. Y-m-d H:i:s , it'll show it in UTC.
How to convert date to another timezone in PHP?php $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); echo $date->format('Y-m-d H:i:sP') . "\n"; $date->setTimezone(new DateTimeZone('Pacific/Chatham')); echo $date->format('Y-m-d H:i:sP') .
How to format date time in PHP?The PHP date() function is used to format a date and/or a time.. H - 24-hour format of an hour (00 to 23). h - 12-hour format of an hour with leading zeros (01 to 12). i - Minutes with leading zeros (00 to 59). s - Seconds with leading zeros (00 to 59). a - Lowercase Ante meridiem and Post meridiem (am or pm). How do I convert UTC time to my time?(GMT-5:00) Eastern Time (US & Canada)
Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.).
|