Solved

How to check Date/Time against another Date/Time

Posted on 2004-10-22
181 Views
Last Modified: 2012-05-05
Hi,

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

Thanks
0
Question by:netele
    6 Comments
     
    LVL 27

    Expert Comment

    by:Diablo84
    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
     

    Author Comment

    by:netele
    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
     
    LVL 27

    Expert Comment

    by:Diablo84
    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
     
    LVL 27

    Expert Comment

    by:Diablo84
    Once you pull it out of the database*
    0
     

    Author Comment

    by:netele
    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
     
    LVL 27

    Accepted Solution

    by:
    <?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

    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