I'm successfully fetching yesterday's mySQL data using
SELECT COUNT(*) as total FROM track
WHERE FROM_UNIXTIME(date,'%Y-%m-%d %h:%m:%s') > DATE_ADD(NOW(), INTERVAL -2 DAY)
AND FROM_UNIXTIME(date,'%Y-%m-%d %h:%m:%s') < DATE_ADD(NOW(), INTERVAL -1 DAY)
However, it uses server's time zone. My server is located in US, If visitor is from a different timezone than US (ex:asia or europe) my yesterday data won't be correct for user. I want to fetch the correct yesterday results based on visitor's time zone. I can get the visitor timezone in php, but I can't figured out how to use it in mySQL.
Don't store times as UNIX timestamp, but instead as TIMESTAMP. Then you can simply set time_zone and everything will be converted as you wish:
SET time_zone = '+10:00';
SELECT COUNT(*) AS total
FROM track
WHERE date > NOW() - INTERVAL 2 DAY
AND date < NOW() - INTERVAL 1 DAY
Otherwise, you can use CONVERT_TZ():
SELECT COUNT(*) AS total
FROM track
WHERE date > UNIX_TIMESTAMP(CONVERT_TZ(NOW() - INTERVAL 2 DAY, '+3:00', '+10:00'))
AND date < UNIX_TIMESTAMP(CONVERT_TZ(NOW() - INTERVAL 1 DAY, '+3:00', '+10:00'))
Related
Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.
I have two columns in my database named dtp_s and dtp_e. Both of these columns hold strtotime() formatted ints which I then use in my PHP application to calculate hours/minutes between time intervals.
I want to display the 5 most recent records in date order, which works fine when I use this:
SELECT id
FROM _records
ORDER BY dtp_s DESC
LIMIT 5
However, I now want to convert the dtp_s back to a DateTime format in my Query and only pull out the data for that week. The issue I have is the records are for a weekly quota, my idea of pulling 5 records out covers Monday-Fri (which is all that is needed and uploaded) however, the following Monday will show the previous weeks Tuesday, Wednesday, Thursday and Friday as well.
I tried to use date_sub for a one week interval but this seems to only work on DateTime datatype columns, not a Unix timestamp:
SELECT id
FROM _records
WHERE dtp_s > DATE_SUB(NOW(), INTERVAL 1 WEEK);
ORDER BY dtp_s DESC
LIMIT 5
How only select the data that is from the current week by converting my formatted DateTime back to DateTime format? I appreciate any help in advance.
An example of my dtp_s and dtp_e is: 1595570400 1595584800
You can convert the filter value to a unix timestamp with date function unixtimestamp(), like so:
where dtp_s > unix_timestamp(now() - interval 1 week)
Actually, you can directly use unix_timestamp() with no conversion:
where dtp_s > unix_timestamp() - 7 * 24 * 60 * 60
Although unix_timestamp() can be very useful, unix_timestamp(now()) is actually redundant. You can just do the whole calculation in the domain of unix timestamps.
I am working in an application where we storing doctor appointments schedule weekday basis. For this purpose, we maintaining a schedule table and a calendar table that stored the date.
Here is the table snapshot of the schedule table
In our application, when a user selects a date the doctor's appointment list is showing based on that date dayname. Now I am getting date range wise required data of a doctor like the following query where no timezone used.
select datefield as date,
(select count(id)
from appointment_schedules
where substring(dayname(calendar.datefield),1,3) = appointment_schedules.week_day
and user_id=$user_id
)
from calendar
where datefield between '$start_date' and '$end_date'"
Now we implementing timezone wise appointment list where the appointment list will be shown based on user timezone. Since the weekday is fixed in the schedule table, I don't find out the correct way how to retrieve considering timezone. For example, if the user timezone difference is 8 hours from the server then in a certain period user day will ahead or behind from the server. I am stuck on how can I show user timezone day-wise accurate data in a single query.
Any suggestions will be highly appreciated.
Thanks in advance
Some rough code below for how you can handle it, I haven't tested this but it should give you enough to fix your queries.
PHP:
$utc_difference = -12;
$week_day = "SUN";
$prev_week_day = "SAT";
$next_week_day = "MON";
MySQL:
WHERE
(DATE_SUB(start_time, INTERVAL $utc_difference HOUR) < 0 AND week_day = $prev_week_day)
OR
(DATE_SUB(start_time, INTERVAL $utc_difference HOUR) > 24 AND week_day = $next_week_day)
OR
(DATE_SUB(start_time, INTERVAL $utc_difference HOUR) > 0 AND DATE_SUB(start_time, INTERVAL $utc_difference HOUR) < 24 AND week_day = $week_day)
I want to make a date interval in a MySQL query. The date is extracted dynamically from an HTML Form and I want to search if something is between 2 days after and after a fixed date. I've made an example but it's not working well.
SELECT * FROM `sessions` WHERE `start_date` = '2014-05-12'
or `start_date` between DATE_SUB(start_date, INTERVAL 1 DAY)
and DATE_ADD(start_date, INTERVAL 1 DAY)
Any other propositions ?
If I'm not wrong every time this query is resulting all rows from the sessions table. Let's take a look at this particular part of the query,
start_date between DATE_SUB(start_date, INTERVAL 1 DAY)
and DATE_ADD(start_date, INTERVAL 1 DAY),
So if the start_date value of a row is 22-07-2014 the query translates to,
start_date between 21-07-2014 and 23-07-2014)
So every row satisfies the condition as every date x is between x - 1th day and x + 1th day.
So if you want to execute the query to find a date between a certain interval, try
start_date between DATE_SUB('$start_date', INTERVAL 1 DAY)
and DATE_ADD('$start_date', INTERVAL 1 DAY)
instead. Note that '$start_date' is a php variable that is the date you are comparing the row with.
I am using PHPMyadmin and putting values in a database using PHP. I store the expiry date of products using a timestamp as follows,
FOR EXAMPLE:
2012-11-04
I want to select all where the expiry date is equal to todays date plus 8 days (such as the one above)
I also want to select all where expiry date is equal to todays date + 2 weeks in a seperate page if any one could help me out would be very grateful!
You can do that with a query like this:
SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY)
You can use DATE_SUB for dates in the past.
Select all where the expiry date is equal to todays date plus 8 days
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 8 DAY)
Select all where the expiry date is equal to todays date plus 2 weeks
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
These docs will be helpful for you:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add