I have a query that produces a result like this:
The data is sorted by date DESC, then by time DESC, but also the common 'markers_name' elements are chunked together. (They're only chunked together for each date).
To do this I get the list of MAX(time)
values for every combination of (markers_id, date)
in conditions
, then join that list to the row set I am getting from the present query, and use the MAX(time)
values for sorting:
SELECT
m.name AS markers_name,
c.time AS conditions_time,
c.date AS conditions_date,
s.name AS station_name
FROM markers m
INNER JOIN conditions c ON c.markers_id = m.id
INNER JOIN station s ON c.station_id = s.id
INNER JOIN (
SELECT
markers_id,
date,
MAX(time) AS time
FROM conditions
GROUP BY
markers_id,
date
) mx ON c.markers_id = mx.markers_id AND c.date = mx.date
ORDER BY
c.date DESC,
mx.time DESC,
m.name DESC,
c.time DESC
The date and time is stored in the MySQL database as UTC time. The client uses javascript to convert the date and time to the local time zone. Instead I'd like to do the conversion on the server with php and MySQL. Can anyone offer any suggestions on how I might accomplish this with a MySQL query (or perhaps with php and MySQL)?
The client uses ajax to request this data from the server so I can easily include timezone offset in the query using the javascript method getTimezoneOffset().
The date is stored into the database using separate date and time fields using this code:
// convert timestamp to seconds
// time is provided by client as the number of milliseconds since January 1, 1970 00:00:00 UTC.
$utc_timestamp = trim($_POST['utc_timestamp']);
date_default_timezone_set('UTC');
$utc_timestamp = $utc_timestamp/1000;
$time = date("H:i:s",$utc_timestamp);
$date = date("Y:m:d",$utc_timestamp);
If necessary I could store $utc_timestamp instead of $time and $date, but it's a little scary to change the way I've been storing my data.
Thanks in advance....