Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to check Date/Time against another Date/Time

Posted on 2004-10-22
Medium Priority
Last Modified: 2012-05-05

I have $dateTime1 and $dateTime2. How do I check to make sure $dateTime1 is after $dateTime2?

Question by:netele
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
  • 4
  • 2
LVL 27

Expert Comment

ID: 12385816
What format are these times in? timestamp/a formatted date etc?

Presuming they are a formatted date this may work for you (depending on the format)

$date_a = strtotime($dateTime1);
$date_b = strtotime( $dateTime2);

//you now have two timestamps which are in an easy format to compare

if ($date_a > $date_b) {
 //the first date is after the second date

Author Comment

ID: 12385846

That's the thing - what format should I use? I'm pulling this info from a database so the most convenient for me is "mm/dd/yyyy hh:mm:ss PM" Can I use this format?

I looked at what you mentioned above and this works great if I were just looking at dates, but I need Date and Time.

Thanks again,
LVL 27

Expert Comment

ID: 12385882
date and time should work fine too.

In my opinion the best format to store a date/time in is a unix timestamp.

This is a ten digit number which represents the number of seconds since the Unix epoc (Jan 1st 1970) is called a timestamp and can be very easily comparred to another timestamp and formatted into a readible form using the date function.

The database field type should be either timestamp or int(10).

You can get the current timestamp using php using the time function ->

$timetamp = time();

Once you pull it out of the timestamp you can then compare it to another stamp just using:

if ($stamp_a > $stamp_b) {
 //the first date is after the second date

and then you can format the timestamp using date();


eg. if your timestamp was in the variable $var

echo date("Y-m-d",$var);

If the timestamp was from today it would output:

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 27

Expert Comment

ID: 12385886
Once you pull it out of the database*

Author Comment

ID: 12385922

Let me try to explain better...

I get these values from the database:

$dateTime1 = "10/22/2004 4:05:00 PM"
$dateTime2 = "10/25/2004 5:00:00 AM"

How do I check to make sure ($dateTime1 > $dateTime2)?
LVL 27

Accepted Solution

Diablo84 earned 1000 total points
ID: 12385951
$dateTime1 = "10/22/2004 4:05:00 PM";
$dateTime2 = "10/25/2004 5:00:00 AM";
if (strtotime($dateTime1) > strtotime($dateTime2)) {
 echo "First time is later then the second";
else {
 echo "Second time is later then the first";

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
Suggested Courses

597 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