How to compare DateTime values?

Posted on 2014-10-13
Last Modified: 2014-10-13

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?
Question by:evibesmusic
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
  • 2
  • 2
LVL 34

Accepted Solution

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?

Author Comment

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.
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?
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 110

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.

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:

Author Comment

ID: 40378516

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.

LVL 34

Expert Comment

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

Expert Comment

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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

630 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