Verify if there are any time periods that fall within the specified start and end times

In my database, there are two fields named start_time and end_time. I need to check if a user-entered value falls within the time range specified by these two fields. If it is, then my function should return true; otherwise, false. I have attempted to write a query for this purpose, but it seems to be malfunctioning. The start time is in AM/PM format (e.g., 7:30 AM for start time and 9:30 AM for end time), which I converted accordingly. Can someone assist me in resolving this issue?

$this->db->group_start()->where('TIME(start_time) <=', date('H:i:s',strtotime($start_time)))->where('TIME(end_time) >=',date('H:i:s',strtotime($end_time)))->group_end()->or_group_start()->where('TIME(start_time) <=',date('H:i:s',strtotime($end_time)))->where('TIME(end_time) >=',date('H:i:s',strtotime($start_time)))->group_end();

Answer №1

If you want to compare times in a MySQL Query, make sure all entries are in the same format.

$time = "9:20 PM";
$this->db->where('start_time <=', $time);
$this->db->where('end_time >=', $time);

Here is the raw query:

SELECT * FROM `table` WHERE `start_time`<='9:30 PM' AND `end_time`>='9:30 PM'

https://i.stack.imgur.com/abcd123.png

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

Transform JSON arrays into JSON structure

I'm currently facing an issue with converting JSON arrays to JSON format data. The output I am currently getting looks like this: https://i.stack.imgur.com/SW2NW.png However, I would like my output to be in the following format: https://i.stack.img ...

PHP: Show only in the "Today" section, not in the "Yesterday" section as well

$today = time() - (3600*24); $Yday = time() - (3600*48); $getMsgsToday = mysql_query("SELECT * FROM users_msgs WHERE uID = '$USER' AND date > $today"); $countToday = mysql_num_rows($getMsgsToday); $getMsgsYday = mysql_query("SELECT * FROM us ...

What other function can I combine with the foreach function?

I am working on populating the empty array named $Errors with specific items to enforce restrictions on my file upload form. My approach involves adding a <div> element as an item within the array, containing certain strings. I aim to concatenate t ...

Submitting a form via email without the need for PHP

Currently, I am focusing on a project that is small and emphasizes presentation more than security. The main objective is to create a form where users can input data, then click submit for all the information gathered to be sent to a specified 'mailt ...

Retrieve the most recent row from a PHP JSON response

Hey there! I've been attempting to run this code snippet using AngularJS, but all I seem to get is the last row of the dataset. I have come across similar examples on various websites, so I'm not sure if there's a configuration setting that ...

What is the reason behind the validity of this statement in PHP?

Can someone please explain to me why the PHP statement `(0x0F | 0xF0)` results in 'whaaa?' being echoed, shouldn't it be `0xFF` instead? If ((0x0FFFFFFF | 0xF0FFFFFF) != 0xFFFFFFFF) { echo 'whaaa?'; } ...

PHP constructor with initialized class attribute fails to retain value

Currently, I am trying to include a class in my project. After setting up the class attribute in the constructor, it appears that the class is not keeping the assigned value. When I manually set the attribute value within the method, everything works as e ...

Encountering a Laravel issue within my application. Each question is followed by a subsequent question based on the provided answer. All questions and corresponding answers are stored in the database

I am brand new to Laravel and databases as I work on creating a web application for individuals facing difficulties with specific products. The app prompts users with questions and based on their answers, directs them to the next question in sequence. Each ...

Retrieve numerical values from a JSON file using PHP

The code I currently have is causing an issue: $json = file_get_contents('https://api.coinmarketcap.com/v1/ticker/?limit=0'); $coins = json_decode($json, true); foreach($coins as $coin) { echo $coin->24h_volume_usd; } Upon running the scri ...

Encountering problems with storing data containing diacritics in a Java REST API integrated with a MySQL database

I've been working on an app and I'm facing some challenges with handling data that contains diacritics or other UTF-8 characters. The specific characters causing issues for me are: ă-Ă-â-Â-î-Î-ş-Ş-ţ-Ţ. Initially, there is an input fiel ...

Exploring ways to utilize Laravel's Auth class with alternate tables

Recently, I added a simple login and registration system to my website and incorporated some additional columns into the default users table. However, I am faced with a dilemma as I also have another table called admin while Laravel's default authenti ...

Retrieving cover images using PHP from Google Books API

Is there a way to retrieve just the cover image from Google Books service using PHP? I tried fetching the page with file_get_contents but it gets the entire book webpage instead of just the thumbnail. I know I can use the src attribute of an img element in ...

Saving user information in PHP files

While delving into a PHP book, I came across a question about storing web form data in PHP pages. I understand the concept of creating a PHP file and receiving data using $_POST (or similar methods), but I'm concerned that this data would be overwritt ...

Is there a way to transfer a significant volume of data from one webpage to another without relying on the POST

Currently, I am utilizing a web server framework that exclusively operates with GET requests. Presently, my task involves transferring a substantial volume of data (specifically the text content in a textarea) inputted by users onto another page where it i ...

Incorporate External Data Source into Magento Admin Products Grid

I'm working on creating a split screen view with one grid displaying products from a remote repository, and another grid showing products from a local repository. To simplify things, all I really need help with is incorporating the remote source into ...

Issue with Ajax communication to PHP script causing data not to be sent

I am developing a web application that functions as an extensive form which will later be converted into a PDF document. Upon submission of the form, all input data is sent to a PHP file where they are stored as variables to be included in the PDF. Some of ...

PHP query will execute even in the absence of clicking the button

I'm encountering an unusual issue. I've defined a query to insert two names into the database, and I've used Javascript(Jquery) to ensure it only runs when the create button is clicked. However, the script seems to be executing every time I ...

Sharing Data Between Pages in PHP Using Variables

I've been struggling with this issue for a while now and despite trying everything, I still can't seem to pass variables to a second page successfully. On the first page: <form method="post" name="form1" id="form1" enctype="multipart/form-da ...

What is the best way to send variables from JavaScript to PHP while utilizing Ajax technology?

While I have noticed similar questions like this one before, I want to address my specific concerns. In previous examples, the questioner referred to using Ajax in a format similar to this: $.ajax({ type: "POST", url: 'logtime.php', ...

Retrieving data from every row in a table using MySQLi for a specific column in PHP

I'm currently facing an issue with populating an array with ID numbers fetched from a MySQL database using PHP. Despite trying the following approach, the array remains empty after attempting to add the IDs retrieved from MySQLi. Can someone help me i ...