• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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
0
netele
Asked:
netele
  • 4
  • 2
1 Solution
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now