Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8287555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:05:02+00:00 2026-06-08T12:05:02+00:00

I am trying to modify my existing code that returns from DB the rows

  • 0

I am trying to modify my existing code that returns from DB the rows that are between a specific date and time. It does the job right, but I would like to add a feature as described below.

This is an example of my DB

           start                  end
1st row    2012-07-24 18:00:00    2012-07-28 17:00:00
2nd row    2012-07-25 19:00:00    2012-07-30 19:00:00
3rd row    2012-07-25 22:00:00    2012-07-28 13:00:00
4th row    2012-07-25 23:40:00    2012-07-27 18:00:00
...
...
...

When my input is 2012-07-25 23:00:00 BETWEEN start AND end ORDER by start DESC

So the results are,

3rd row
2nd row
1st row

Until here everything is great. However, I want to list any rows that are going to begin in the next 2 hours at the top of the list.
This is row number 4.

I tried to do it using

 ABS(TIMESTAMPDIFF( HOUR , `start`, :time )) < 2

unfortunately, I can’t find the solution.

Below is my code since now, any help is appreciated. ( I use 3 variables, selected, city and type)

// Store where clauses and values in arrays
$values = $where = array();

if (!empty($selected)) { // 
    $where[] = ':selected BETWEEN `start` AND `end`';
    $values[':selected'] = $selected;
}

if (!empty($city)) { // 
    $where[] = '`city` = :city';
    $values[':city'] = $city;
}

if (!empty($type)) { // 
$where[] = '`type` = :type';
$values[':type'] = $type;
}

// Build query
$question = 'SELECT * FROM `events`';
if (!empty($where)) {
    $question .= ' WHERE '.implode(' AND ', $where);
    $question .= ' ORDER BY `start` DESC';
}

$query = $db->prepare($question);

$query->execute($values);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:05:03+00:00Added an answer on June 8, 2026 at 12:05 pm

    I didn’t check the order of your arguments, so I got the condition backwards. However, my initial statement still stands. Basically, the simplest answer is to adjust your starting time, like so:

    SELECT * 
    FROM TMP
    WHERE startedAt < DATE_ADD(TIMESTAMP('2012-07-25 23:00:00'), INTERVAL 2 HOUR)
    AND endedBy >= TIMESTAMP('2012-07-25 23:00:00')
    ORDER BY startedAt DESC
    

    And have a working example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update (add/modify files) an existing JAR file, and this code (using
I am trying to modify an Integer field on existing table from nullable to
I'm trying to use django-evolution to modify some models from an existing project. Now
I'm trying to modify an existing Pages document from within my application using the
I was trying to modify an existing code to rotate an image based on
I am trying to modify an existing WPF application for localization. One of my
I'm trying to modify an existing NSIS install script to allow for different licence
I am trying to modify an existing MySQL database for use in a new
I am trying to modify the script below to click a button that looks
I'm trying to modify and existing Access application to use MySQL as a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.