how to check whether time is in between given range of time intervals

Posted on 2013-10-22
Last Modified: 2013-10-22
I want to compare a db column which stores time in format 09:30:14 against a php variable which stores time in same format.I want to check whether the time stored in dbcolumn is less than 2 miutes of time stored in php using mysql.I want to retireve some values based on this check
    $ordertime_present = 09:32:14;
    $ordertime_before = 09:30:14;
    $sql = "select count(keyid) as key_id_count from key_master where orderdate='$orderdate'
           and ordertime between '$ordertime_present' and '$ordertime_before'";
Question by:srikanth_saladi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 110

Expert Comment

by:Ray Paseur
ID: 39591077
LVL 110

Accepted Solution

Ray Paseur earned 500 total points
ID: 39591089
It would seem more natural to me to have the older time first in the between clause.  But that aside, why not combine the orderdate and ordertime into a single DATETIME column?  That would give a better design in my opinion.

$ordertime_present = date('H:i:s');
$ordertime_before = date('H:i:s', strtotime("NOW - 2 MINUTES"));
$sql = "select count(keyid) as key_id_count from key_master where orderdate='$orderdate'
           and ordertime between '$ordertime_before' and '$ordertime_present'"; 

Open in new window


Author Closing Comment

ID: 39591283
Thanks Ray_Paseur,

I made a mistake by not putting older time first.Once i changed it works for me
LVL 110

Expert Comment

by:Ray Paseur
ID: 39591426
I see you're new here, @srikanth_saladi.  Please review the grading guidelines.  Since the "default" grade is "A", when you give a lower grade you are also expected to explain why the answer was deficient.

If you want to change the grade, just post back.  Thanks and regards, ~Ray

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
history of pages 7 55
Google Recaptcha 13 60
mysql workbecn having problems to export tables to cvs 4 29
replacate wordpress websites 3 24
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question