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');
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
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.
My date is ahead 1 hour when I post to mySQL via php. I'm in Los Angeles. How can i make the time correct?
Here is what I currently have:
$date = date("m/d/y g:i A") ;
Use this to get the time zone PHP is using date_default_timezone_get();
If you're in Los Angeles you can set your time zone temporarily for only the current script
date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Also pass T in the date function:
echo date("D M j G:i:s T Y"); outputs Mon May 25 16:23:49 EDT 2009
see
date_default_timezone_set
like
date_default_timezone_set('America/Los_Angeles');
First check if servers time is correct ( on linux use date shell command). If yes, you just set the time and you're good.
If not you have to change it in php with setlocale() or date_default_timezone_set() function
It depends on your specific case, but I had saved the date as UTC / GMT time, so I had to use the gmdate function instead of date. E.g.
gmdate("H:i",$time)
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