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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
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.
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 …

752 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