Fetching data from MySQL based on the day of the week and specific timezones

I'm currently working on an application that stores doctor appointments on a weekday basis. To manage this, we have a schedule table and a calendar table that stores the dates. Below is a snapshot of the schedule table: https://i.stack.imgur.com/OPRPb.png

Within our application, when a user selects a date, the list of doctor's appointments for that specific day is displayed based on the day name. Currently, I am extracting the necessary data range wise using the query below, without considering time zones:

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'"

We are now working on implementing a timezone-based appointment list display, which will show appointments based on the user's local timezone. Since weekdays are fixed in the schedule table, I am facing challenges in retrieving accurate data considering different timezones. For instance, if there is an 8-hour difference between the user's timezone and the server, the user's day may be ahead or behind the server's day within a certain period. I am struggling to figure out how to display user timezone-specific day-wise data accurately with a single query.

I would greatly appreciate any suggestions or guidance on this matter. Thank you in advance.

Answer №1

Check out the code snippet below for handling the situation. While I haven't run it through testing, it should provide you with a good starting point to resolve your query concerns.

PHP:

$utc_difference = -8;
$week_day = "WED";
$prev_week_day = "TUE";
$next_week_day = "THU";

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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with the functionality of Jquery Ajax Script

I am encountering an issue with my ajax/php script. It is designed to check the availability of a Username for user registration. script $(document).ready(function() { $("#username").change(function() { var usr = $("#username").val(); ...

Utilizing Bootstrap Modal to Display PHP Data Dynamically

Modals always pose a challenge for me, especially when I'm trying to work with someone else's code that has a unique take on modals that I really appreciate (if only I can make it function correctly). The issue arises when the modal is supposed ...

The JQUERY code for refreshing a div requires a timeout delay

I'm looking for a way to refresh a specific div on my website that's used for chat. Here's the code I currently have: var refreshId = setInterval(function() { $('#chat_grab').load('chat_grab.php?randval=' + Math.rand ...

What's preventing me from retrieving the file content for this website using file_get_contents in PHP?

Greetings to you. I have recently created a PHP tool that can extract YouTube video details of all public videos in JSON format. The tool functions flawlessly on both local and live servers. However, I encountered an issue when attempting to retrieve conte ...

After refreshing the div, Ckeditor fails to display

I'm currently facing an issue with assigning an initial value to a ckeditor using a jQuery adapter in PHP. Whenever jQuery refreshes the div containing the ckeditor, the ckeditor disappears. Here is how I've defined the editor: $ckeditor = new ...

Streaming SQL Server to Node.js using the MSSQL library

I am recently starting to explore the world of node.js and I'm currently working on establishing a connection to SQL Server using the mssql library. However, I find the example provided in the git repository to be quite vague, leaving me uncertain abo ...

The processing time for this request is unreasonably long

My website is built using Laravel and Vue.js. I am facing an issue where a function takes more than 2 minutes to execute, resulting in the following error: 500 Internal Server Error Request Timeout This request takes too long to process and is timed out b ...

Increase the efficiency of MySQL InnoDB queries in your PHP application

I've encountered some issues with the code in my PHP/MySQL application. While it generally runs smoothly and takes about 3-4 seconds, the first execution (per session) can take up to 2 minutes. I suspect this delay is due to certain automated cache me ...

What is the process for determining the area code of a location based on the postal code/zipcode?

Currently, I am in the process of developing an app's payment system that requires me to incorporate Canadian GST and HST taxes based on the customer's zipcode. This is necessary because my shipping method calculates shipping charges using the pr ...

Assistance requested for utilizing stripos() and substr() functions

Is there a way to remove everything before the word "sentido" in this given string? $string = "#BLitz da #PM na est do pontal sentido prainha perto do camping"; The desired new string is: $string = "prainha perto do camping"; I have tried using stripos ...

Trouble Connecting to Node.js MySQL Pool Using Async Series

Below is a snippet of my node script: pool.getConnection(function(err, maria_connection) { maria_connection.query(' CREATE TABLE Currency(uuid VARCHAR(10),name VARCHAR(12),PRIMARY KEY(uuid), UNIQUE(name))') async.series({ ...

Pass arguments in an ajax request when making a request with jQuery datatables

My goal is to populate a jQuery datatable using ajax and pass parameters to the function retrieving data from the database. This is what I want to achieve: $('#datatables').dataTable( { "bProcessing": true, "bServerSide": true, "sAj ...

One omission of argument 1 has been detected in the search_model::autocomplete() function

This is the controller for the search.php file. public function login() { $email=$this->input->post('email'); $password=md5($this->input->post('password')); $result=$this->search_model->login($email,$passw ...

Creating multiple thumbnails and storing them in various folders using CodeIgniter

I am looking to organize my images into different folders - one for original pictures and another for thumbnails. However, when I try to upload an image to the thumb directory, it shows up as empty. https://i.stack.imgur.com/5Fybz.png Below is the code s ...

Unable to sign up for WordPress function

I'm having trouble getting my function registered properly in WordPress, no matter how many times I try. So far, here's what I've done: Inserted code into themes functions.php function test_my_script() { wp_register_script( 'custom-s ...

The Ajax confirmation prompt seeks a singular piece of information

After implementing an ajax beforeSend function with a confirm() statement, I encountered a problem. The issue is that when attempting to delete the first value, it correctly prompts for confirmation. However, if trying to delete the second, third, or fourt ...

Having trouble decoding JSON data using PHP

I am currently attempting to parse JSON data for a project I am working on, but I am facing some issues. The code I previously used is not functioning properly with this particular API. Below is the code snippet: <?php $json_string = file_g ...

A critical issue occurred: Attempting to call a method on a null object in the system

Hey there! I'm currently working on a PHP form to input values into my database table. Here's the code I've put together: <?php $host = "localhost"; $db_name = "my_farneseluca"; $username = "farneseluca"; // Establish connection ...

``Please proceed with the form submission only if it has been verified and

Within my web application, there are several pages that handle submitted data from forms. I would like to prevent the following scenario: A user creates a form on the client side with identical fields to my original form and sends it to the URL responsibl ...

Unordered calling of functions in JavaScript - is it possible?

I'm currently working on a project that involves extracting data from an SQL database and converting the output of a query (which is a number) into a corresponding color, which is then passed to a JavaScript variable. Essentially, I am using ajax to ...