Mysql is not converting timestamp to the timezone of PHP query - php

I have a mysql database in this format
And I am trying to fetch the values through a php document and convert them into json on the timezone of the user (or maybe just GMT-6 would suffice) but the json outputs from the php document are as follows:
[{"timestamp":"2018-06-13 19:52:05","temperature":"79.83","humidity":"41.89","pressure":"99.35"},{"timestamp":"2018-06...
Still in UTC time, I have tried adding
date_default_timezone_set('America/Los_Angeles');
To the php document, but the time never changes, how would I solve this?

Use convert_tz() on the timestamps in your query to convert them from one timezone to another, e.g.:
... convert_tz(`timestamp`, 'Etc/UTC', 'America/Los_Angeles') ...
Make sure to follow the procedure "Populating the Time Zone Tables" as described in "5.1.12 MySQL Server Time Zone Support" to have the time zones available or to check which are available in your system.

Related

Read date from Core Data attribute

I have a sqlite database generated by Core Data.
I need read a field type Date with PHP and convert it into DateTime. The field in Core Data has a value like: 631170000.
<attribute name="date" attributeType="Date" defaultDateTimeInterval="-978278400" usesScalarValueType="NO"/>
How can I transform this defaultDateTimeInterval double value into a Date?
If you're reading Core Data persistent stores in PHP, you're setting yourself up for difficulty, because those files are not designed to be read directly. Core Data is not simply a SQLite wrapper, and the structure of the tables and data types it uses in SQLite are not documented and may not be what you expect. If it's at all possible to read the data using Core Data and then send it to your server running PHP, do that, because otherwise you'll end up reverse-engineering parts of Core Data since you're directly reading a file not designed to be used that way.
Dates are one of the simpler cases. Core Data saves them as the number of seconds since midnight on December 31, 2000, UTC. It's not a Unix timestamp but it's the same idea with a different reference date.
There might be a PHP library or some open source code to convert. If not, the difference between Apple's reference date and a Unix timestamp is 978307200 seconds. To convert the number in your question, add 631170000 + 978307200 and treat the result as a Unix timestamp.
But really, if there's any way you can use Core Data to read and convert the data, do that instead, you'll save yourself trouble.

How to get '2017-08-29T09:43:42.335Z' date format in PHP?

How can I get 2017-08-29T09:43:42.335Z date format in PHP.
And what does T09:43:42.335Z correspond to?
T09:43:42.335Z is the time (including milliseconds) in the Zulu timezone (the T is just a separator).
This is not a time format that's built-in in PHP. As of PHP 7.0.0 you could make it yourself like so:
$date->format('Y-m-d\TH:i:s.v\Z');
In earlier PHP versions there is no "milliseconds" option, so you would have to concatenate that yourself.
The Zulu timezone is equal to UTC+0. Make sure you convert your time to UTC+0 before you print this out, the format command won't do that for you.

Best way to store date and time with MySQL PHP + AJAX

I'm making a web based project management application using MySQL and PHP that uses JS (Jquery) in the front end. The user has to input a date and optionally time as well.
However I'm not sure how I should go about inserting and storing the date and converting it back to human readable form in the application.
Thanks in advance,
RayQuang
Always use the standard Date/Time types for the respective situation.
In MySQL, use one of the appropriate Date and Time Type. Don't just blindly use one type. If you're storing a date, don't use a timestamp. If you're storing a timestamp, don't use a date. Use the proper type and be done.
In PHP, you can use an integer (parse from mysql's type with strtotime().
Talking with JS, I'd suggest using RFC 2822 date format, since it's standard. That way, you're communicating externally using a standard date/time format (which is non-ambiguous).
Store as timestamp.timestamp contain both date and time. and it will be best way to store date and time .

How do I show datetime in the same time zone as user using PHP or javascript?

Suppose now I've got the datetime to show like this:
2010-05-29 15:32:35
The the corresponding time zone can be get by date_default_timezone_get,
how do I output the result in the same time zone as user's browser so that users don't get confused?
There is no reliable way to read the user's locale timezone settings from PHP or JavaScript.
In JavaScript you can read the offset between UTC and the time in the user's current timezone, but that doesn't give you a timezone name, so you're left either leaving the timezone identifier off (making the times completely ambiguous) or including it is an explicit offset like UTC+01:00, which is ugly, and still doesn't take care of changing timezones over different DST periods.
As bobah says, you can use toLocaleString() on a JavaScript Date to output it in the client's real desktop timezone. However, this way you get no control at all over the date formatting. For example on my machine Chrome outputs the unwieldy:
Sat May 29 2010 15:03:46 GMT+0200 (W. Europe Daylight Time)
Whereas Opera just coughs up:
29/05/2010 15:03:46
Which, as it doesn't state the timezone at all, is uselessly ambiguous. IE is similarly terse, and Safari states no timezone either. Firefox does for me on Linux, but not on Windows. Argh.
So if you need reliability the only way to handle timezones is to let the user manually choose one in their site settings, then convert from UTC to that timezone when you're producing a page for them. You can make a guess in JavaScript as to which the most likely of some common timezones it might be (see this question for strategies), but you can't guarantee you'll be right.
You can pass UTC timestamps to the page and convert them with JavaScript. I used this trick once and was happy with the result. There is a constructor of JavaScript Date taking UTC timestamp. For UTC timestamp generation from PHP one may use gmmktime().

PHP: storing/displaying relative dates in UTC

I have a list of dates stored in MySQL using PHP.
These were stored using the following code:
date_default_timezone_set('UTC');
$strDate = date("Y-m-d H:i:s",time());
I can only test this from my timezone, which is also UTC!
If a web visitor from eg Eastern Time USA views the page, will the date be converted to UTC correctly?
Presuming that I am storing the UTC dates correctly, what PHP function will display the UTC time, converted to the user's own timezone??
Firstly you're right to store the UTC; however remember that the date you have in PHP will be the server date - not the client date.
So to continue, read up on how to extract timezone based dates. Then consider how to extract the timezone from the browser - which you will need if you do the local timezone output in PHP rather than on the client.

Categories