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)
Related
I am creating a booking management system. I got stuck in the booking cancellation part. I want to allow users to cancel their orders if their booking time and the current time duration is between 2 hours because I want to restrict the users to cancel their booking if their booking time and current time duration is greater than or equal to 2 hours.
I want to generate a query that returns all the bookings whose booking time is less than 2 hours. How can I achieve this?
This is my database structure.
SELECT * FROM `TableName` where TIMEDIFF(NOW(),Your_date_ColumnName) < '02:00:00.000000'
Assuming that booking_time is in MySQL standard format.
Try this and the below query will use index if you have one in booking_time column
SELECT *
FROM booking_table
WHERE booking_time BETWEEN CURRENT_TIMESTAMP() - INTERVAL 2 HOUR AND CURRENT_TIMESTAMP()
You can extract the hour part of your date. Refer to this link
Then using this query to get those less than 2 hours.
SELECT * FROM table1 WHERE tdate - 120 < EXTRACT(MINUTE FROM now())
I currentluy use this custom SQL in Contao SQL to display all entries (metamodel) that are in the future.
SELECT * FROM {{table}} WHERE party_date > UNIX_TIMESTAMP();
Now when I have a entry (party) which is scheduled for 2017/03/28 it won't be displayed when its 2017/03/29.
But how can I keep this entry up until 2017/03/29 - 04:00am in the morning?
Visitors of the website should see this partry up until 4am in the morning (event site).
Is it possible with UNIX_TIMESTAMP() ?
Assuming you want to have 4 hours' gap, you can subtract 4 hours from current datetime and compare the DATE part of party_date and NOW(), e.g.:
SELECT *
FROM table
WHERE DATE(party_date) >= DATE(DATE_SUB(NOW(), INTERVAL 4 HOUR))
By this logic, 2017/03/29 - 04:00am would result in 2017/03/29 and as it's same as date part of party_date, it will be displayed.
Here's MySQL's documentation for datetime functions.
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'))
I'm working on a invoice system for a application of mine. I want to invoice the users every 2 weeks. There is a cronjob every week with the check if the user gets an invoice. But it give me some bugs, because it has been a new year and the system gives someone a invoice even when they have got them a week ago.
This is my query:
SELECT *
FROM user
WHERE DAY(registered) = DAY(DATE_SUB(CURDATE(), INTERVAL 2 WEEK))
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
Thank you!
You can try this- (code is tested) Demo
SELECT * FROM user WHERE registered > CURRENT_DATE() AND
MOD((FLOOR( DATEDIFF( now( ) , `registered` ))),14) = 0
The day function in MySQL returns the day of month. You will clearly have problems with your query when the time period spans month boundaries.
I think you want logic of this form:
SELECT *
FROM user
WHERE registered >= DATE_SUB(CURDATE(), INTERVAL 2 WEEK)) and
registered < curdate()
(Or <= if you want the current date to be invoiced.)
I do not know what this is doing:
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
If the day of the month is the same for registered and now(), then registered should be the current date. The only difference between now() and curdate() is that the former includes the time stamp. I would also say that it is bad practice to mix three different ways of getting the same date in the same query, even if they are equivalent.
I am currently developing a sports website where one of the pages with be forthcoming fixtures in which the user will be able to what team and where the team are playing their next match.
I have a database with the following fields...
ID
TEAM NUMBER
OPPOSITION
VENUE
DATE
MEET TIME
MATCH TYPE
So a row of data pulled from the DB and print_r'd may look like this
ID=>[1] TEAM NUMBER=>[1] OPPOSITION=>[YORKSHIRE] VENUE=>[HOME] DATE=>[2009/4/25] MEET TIME=>[13.00] MATCH TYPE=>[CUP]
My problem is i cannot work out how to show the next match dependent on what the current date is, so for example for now I want the site to show all the games that will happen over the weeken of the 25th April 2009 and then once that has gone the fixtures for the next weekend.
Hope this makes sense and some one give me an idea of how to tackle this.
select * from my_events where date between now() and date_add(now(), interval 7 day);
Should do it I think.
Instead of relying entirely on MySQL, you can also use PHP's strtotime() function:
$query = "select * from my_events where date between now() and ".
date("Y-m-d", strtotime("+1 week"));
For MySQL check out the Date and Time functions. You can use a combination of CURDATE() and ADDDATE() to achieve what you need.
Your description is very vage but try something like this:
SELECT all_fields_you_need
FROM table_name
WHERE `DATE` > CURDATE() AND `DATE` <= DATE_ADD(CURDATE(), INTERVAL 7 DAY)
ORDER BY `DATE` ASC
(not tested, just written as it came into my mind...)
Load it all into an array and display the data
you can get the system date (in Oracle using sysdate) and then add to it, so look for all records where DATE = sysdate + 7. You may have to play with this a little, formatting the date so that sysdate + 7 returns a date without the time, but that is basically what you need.
EDIT:
If you want the event between now and a week from now (if games are only on the weekend, then this will return next weekend's games) do
DATE > sysdate AND DATE <= sysdate + 7
To get the next match for team xxx
SELECT *
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE = ( SELECT MIN(DATE)
FROM myTable
WHERE TEAM NUMBER = xxx
AND DATE > NOW() )
I suspect this is what you really want, if matches only take place at weekends (which seems to be an assumption from your question).
Today + 7 days is not the same as next weekend unless today happens to be the same day of the week as the match.