Solved

How to compare DateTime values?

Posted on 2014-10-13
7
269 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
Comment Utility
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
Comment Utility
@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
Comment Utility
Can you post some sample data and the fragment of PHP/MySQL code that is not working as you expect?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
Thank you. I am glad you got it sorted.
0
 
LVL 108

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.

728 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

11 Experts available now in Live!

Get 1:1 Help Now