Solved

How to compare DateTime values?

Posted on 2014-10-13
7
272 Views
Last Modified: 2014-10-13
Experts,

I am trying write a MySQL query to compare a static datetime value vs. the datetime values stored in my database. I am having trouble doing so.

The datetime values ("JobStartDate") in my database have the following format: '1899-12-30 xx:xx:xx'

The example query below should demonstrate what I am trying to do:

$query = "SELECT * FROM table WHERE JobStartDate < '1899-12-30 09:59:00'";

Can someone please assist me in correcting my syntax so that the two datetime values are evaluated correctly?
0
Comment
Question by:evibesmusic
  • 3
  • 2
  • 2
7 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 40377647
The basics of your query look OK. I cannot see an obvious syntactic error. Are you getting an error message that leads you to believe your syntax is wrong?
0
 

Author Comment

by:evibesmusic
ID: 40377654
@Beverley Portlock:

I am not getting an error but, I am not getting any results from the query when I know results should exist.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 40377656
Can you post some sample data and the fragment of PHP/MySQL code that is not working as you expect?
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40377720
This article shows some of the ways of dealing with DATETIME values in PHP and MYSQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Key points: All date / time values for internal use should be held in ISO-8601 format.  In the MySQL column definition, this is data type DATETIME.  In PHP this is date('c') or date('Y-m-d H:i:s').  The format is both easy to read and well-standardized allowing comparison and sorting.

If you're using the BETWEEN clause in a query, you may want to be aware of this:
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/A_11210-Beware-of-Between.html
0
 

Author Comment

by:evibesmusic
ID: 40378516
@all:

The column in my database was defined as a "text" data type and not a "datetime" data type. I've updated the data type and now my query works.

Cheers!
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 40378537
Thank you. I am glad you got it sorted.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40378654
Bingo!  Glad it's pointed in the right direction.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Echo vs ?><?php  html code 4 44
subtract 1 in Access 2003 query 7 35
Whether to store ID's or text values for detailed information 3 18
MySQL  on Tomcat 8 22
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
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.

947 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

23 Experts available now in Live!

Get 1:1 Help Now