How to check Date/Time against another Date/Time

Hi,

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

Thanks
neteleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
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
}
0
neteleAuthor Commented:
Thanks,

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,
0
Diablo84Commented:
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();

http://us2.php.net/manual/en/function.date.php

eg. if your timestamp was in the variable $var

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

If the timestamp was from today it would output:

2004-10-22
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Diablo84Commented:
Once you pull it out of the database*
0
neteleAuthor Commented:
OK,

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)?
0
Diablo84Commented:
<?php
$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";
}
?>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.