date_default_timezone_set("Asia/Singapore"); // UTC +8
$dt = new DateTime();
$dt->setTimestamp(DateTime::createFromFormat('u', gmdate('u'))->getTimestamp());
echo $dt->getTimezone()->getName(); // Asia/Singapore
echo $dt->format('d M Y H:i:s'); // Correct local time
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('d M Y H:i:s'); // Correct UTC Time
die;
I am wondering if timestamps contain timezone data. On line 3 you see that I used gmdate() which should give me UTC/GMT time. But when I get the timezone & formatted datetime, they are in localtime. I didn't set timezones in my DateTime object yet, gave it a UTC timestamp. It somehow knew to convert to localtime. Which makes me wonder if Timezone data are included in timestamps
setTimestamp accepts the timestamp in GMT 0 and you passed it in GMT, because of gmdate('u'). DateTime object takes your current timezone by default.
After that - you have properly set timestamp and current timezone, that is why DateTime object formats the date for Singapore.
Which makes me wonder if Timezone data are included in timestamps
No. Timestamp stores just amount of seconds since unix-epoch.
Timestamps are interpreted according to the interpreters timezone.
So no, they are standard and do not contain timezone data within themselves.
your example proves it, as you change the timezone, so does your result.
If the timezone data was embedded, the result would not change.
Related
I used some api which gets the expiry date as
2021-07-09T14:09:47.529751-04:00
How do I process this and store it in the database.
Do I store this as datetime ?
Does it have any timezone associated with it ?
Is this correct but what do I do with the time zone. My server has a different time zone so if I store this value it would not be correct ?
echo date( "Y-m-d H:i:s", strtotime("2021-07-09T14:09:47.529751-04:00") );
The date format you're looking at is ISO 8601.
Strtotime() will convert it into an unix timestamp that is in UTC and you can save it to your database as a number. The number should be larger than 4 bytes so that you don't run in the Year 2038 problem. Everytime you read the value, you must convert it to the proper timezone of the user. This is the easy way. You'll never have to manually fix the dates in the database.
If you want to save it to database as a date, you probably want to still save the date as UTC and apply any transformations when you show the value. Convert it with gmdate() before saving:
echo gmdate( "Y-m-d H:i:s", strtotime("2021-07-09T14:09:47.529751-04:00") );
If you always show the value in one time zone, you can set the timezone in php.ini. This is not the best way though. It's best make it clear what timezone your program is using and explicitly set it in code.
When you want to show the value in other timezones, refer to this question or the manual of settimezone.
In my database I have a datetime field with the following value 2014-07-21 00:00:00.
When I pull the data I have the line:
SELECT UNIX_TIMESTAMP(date) AS `date` ...
When I then use PHP date to format it for human reading the following happens:
echo date('d/m/Y H:i:s', $row['date']);
// outputs 20/07/2014 23:00:00
I don't understand what's doing this.
It's a timezone problem. 2014-07-21 00:00:00 will convert to very different UNIX timestamps depending on what timezone the database assumes this time to be in. Conversely, PHP will convert the UNIX timestamp to the human-readable version depending on the timezone set with date_timezone_default_set. You'll have to ensure that MySQL's internal timezone setting and PHP's internal timezone setting are identical if you expect the same value to be output.
I replicated the same in my database and I got exactly the same value. I cannot say if you and I are in the same timezone, but one thing is sure - according to the documentation Here
If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC
That explains the disparity.
To solve the problem you can select the date normally and use strtotime in php to convert it to timestamp. As in
SELECT date AS `date` ...
echo date('d/m/Y H:i:s', strtotime($row['date']));
Hope this helps..cheers!
The date I store in mysql looks fine when viewing the database, but when I am viewing the date it is always behind a day.
The mysql data type is "date".
I am getting the date column from mysql using UNIX_TIMESTAMP.
I am displaying the date using php's date() function.
I am using bluehost and configured php.ini to use Los Angeles time zone.
Example: I create a new article and key in 2013-05-08. It gets stored as a date type in mysql showing 2013-05-18. But when I go to view it later it shows 2013-05-07.
The timestamp is 1367992800, which is Wed, 08 May 2013 06:00:00 GMT in GMT, but in my timezone Tuesday, May 07, 2013 11:00:00 PM.
How do I display the originally intended date of 2013-05-18? I do not want to change my timezone to another since Los Angeles is my correct time zone and for all my other methods where we need to record a timestamp, it is accurate.
If you want your date to be independent on timezone you should store it in a column that does not transform the value. So the date column type you've chosen is quite appropriate.
The problem comes when you try get that date in UNIX TIMESTAMP. UNIX TIMESTAMP is an amount of seconds since 01.01.1970 in GMT. It means that mysql converts your date into timestamp in GMT using it's timezone settings.
PHP date function also depends on php timezone settings and converts timestamp into date in the current timezone.
To get date from MySQL without converting it into current timezone just do not fetch it using UNIX_TIEMSTAMP function. Get it as is or using DATE_FORMAT function.
You should use the DateTime object.
$timezone = new DateTimeZone('Europe/London');
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->setTimezone($timezone);
echo $date->format('Y-m-d H:i:s');
I want to save to database a given date with this format:
date('Y-m-d H:i:s')
Using datetime datatype, it outputs:
2012-10-29 00:18:14
To check if it gives the correct output, I add to my table a timestamp which returns the current date and time of the insertion/update, it returns:
2012-10-29 07:18:14
I suspect that there is something wrong with my format.
sounds like a timezone issue.
you can use date_default_timezone or the date.timezone ini setting to have PHP use the same timezone as MySQL.
you can see the MySQL timezone with:
select ##session.time_zone;
you definitely want to use this format: date('Y-m-d H:i:s') according to php manual (http://php.net/manual/en/function.date.php) - "H" is used for 24 hour time format, "h" is used for 12 hour format, mysql will use a 24hr clock. MySQL standard format is "Y-m-d H:i:s"
However, you are getting hit with timezone here. I bet you are 7 hrs from UTC. try setting your timezone first:
date_default_timezone_set('UTC'); or whatever timezone you want.
TIMESTAMP is stored in UTC (GMT) http://dev.mysql.com/doc/refman/5.5/en/datetime.html while DATETIME is not, hence your difference.
I have a table with a field of type date within a MySQL database. My user places a date into a field (format dd-mm-yyyy) which I convert into yyyy-mm-dd for insertion into the database. This works fine. I can see the date in there as (for example) 2012-04-04.
My issue is that I then select this record, convert the date to the format I wish to display (dd-mm-yyyy) and get 03-04-2012. I understand why, in that my database is set to UTC, however the user is on Berlin time, therefore 04-04-2012 00:00 in Berlin is 03-04-2012 23:00 UTC.
The issue means that if I then save the displayed date (03-04-2012), the next time I see it, it displays as 02-04-2012 because I saved only the date and therefore the system is assuming a 00:00 time again.
I cannot see a way around this other than setting this as a datetime type rather than a date type, however I would rather not do that as time (for various reasons) is stored in a separate field. Any suggestions?
When you inserting a record you add as datetime current UTC time, after that every user in their profile may want to/or set his timezone.
If you know the timezone of the user u can easy convert the datetime to user locale time. Because you know the differences in hours/minutes between the time.
P.S. You can store the datetime as varchar and save the unix timestamp in this field. Unix timestamp is based on current timezone I think.
UPDATE:
I think that might help
$date = time();
dump(date('d-m-Y H:i:s', $date)); // 03-04-2012 08:43:38
date_default_timezone_set('Europe/London');
dump('London: '. date('d-m-Y H:i:s', $date)); // London: 03-04-2012 11:43:38
date_default_timezone_set('Europe/Berlin');
dump('Berlin: '. date('d-m-Y H:i:s', $date)); // Berlin: 03-04-2012 12:43:38
date_default_timezone_set('Europe/Sofia');
dump('Sofia: '. date('d-m-Y H:i:s', $date)); // Sofia: 03-04-2012 13:43:38
dump function returns '<pre>'. $something .'</pre>';
UTC is the international time standard. It is similar to Greenwich Mean Time (GMT), except that UTC observes no daylight saving time (DST) and is based on a 24-hour clock. Zero (0) hours UTC is midnight GMT. The local 24-hour time convention is converted to UTC by adding or subtracting hours based on location in relation to the prime meridian, as well as local daylight saving time considerations.
First, make sure both time zones are same. Then, don't store in datatime format, use integer. Convert the date to timestamps and then store. Like
$time = time(); //get the current time stamp
//Now insert $time
Now, both places are in common ground, You may do as you like. Changing date among different timezone is rather easy.
echo gmdate("M d Y H:i:s", $time);