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
  • 3
LVL 108

Expert Comment

by:Ray Paseur
ID: 39591077
LVL 108

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 108

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now