How to check Date/Time against another Date/Time

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

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

Question by:netele
    LVL 27

    Expert Comment

    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


    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

    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:

    LVL 27

    Expert Comment

    Once you pull it out of the database*

    Author Comment


    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

    $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";

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP ma…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    846 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

    5 Experts available now in Live!

    Get 1:1 Help Now