hi

I have a session variable $_SESSION['generale']["order_time"] that contains the string "22:00:00" taken from a TIME field in a DB

i need to be able to compare that with the current time that i get with date("H:i:s");

when i compare them using

if(date("H:i:s")>$_SESSION['generale']["order_time"]){

it doesn't work, so i guess i need to do something to the times before being able to compare them

can anyone suggest what i need to do ?

thanks

    Depending on what you are trying to do, it might be easier to do it in your database query. But if you want to compare time strings in your PHP code, you'll either need to compare them as strings using something like [man]strcmp/man, or convert them to numeric values that can be compared such as with [man]mktime/man, [man]strtotime/man, or else [man]explode/man-ing the units and doing some arithmetic to come up with the number of seconds.

      ok you've given me some good ideas there

      I think I'll go for the simplest - explode the string to get the hour digits, turn them into integers with intval() and compare like that

      thanks 🙂

        hi.
        I have another simple compare.
        But one question:
        - What happens when current time is late 23:00:00
        and the time to compare with is next day 02:00:00?

        Dont you have to include the DAY in your compares??

        Anyway, here is my suggestion.
        It uses this compare statement, where $test can be "22:00:00"

        if(str_replace(':','',$test) < date('His'))

        Here is my commented version, and 'The Short Version'

        <?php
        
        $test = '22:00:00';
        // replace semicolon with empty ''
        $comp = str_replace(':', '', $test);
        
        $t = time();
        // will make like: '151720'
        $now = date('His', $t);
        $longnow = date('H:i:s', $t);
        
        // compare strings
        if($comp < $now)
            echo $test.' is less than '.$longnow;
        else
            echo $test.' is greater than '.$longnow;
        echo '<hr>';
        
        ////////////////////////////////////////////////////////
        // Short version, compare with current time
        $test = '06:20:47';
        
        if(str_replace(':','',$test) < date('His'))
            echo $test.' is less than now';
        else
            echo $test.' is greater than now';
        
        ?>
          halojoy wrote:

          - What happens when current time is late 23:00:00
          and the time to compare with is next day 02:00:00?

          Dont you have to include the DAY in your compares??

          not in this case - I just need to know if an order is being placed after a certain time of day so that i can determine if it can be delivered the next day (before the cut-off time) or that day after next (after the cut-off time)

          I got it working with just extracting the hour from the times and forcing the admin to specify a 2-digit number for the cut-off time - but your solution is very smart and more flexible if you need to use minutes etc

          nice one

          and thanks 🙂

            4 years later

            you only need to apply the wrapper strtotime(), ie:
            if( date('H:i') > date('H:i',srtotime($_SESSION['your_time']))){

            }

            Hope this helps!

              crsland;10986336 wrote:

              you only need to apply the wrapper strtotime(), ie:
              if( date('H:i') > date('H:i',srtotime($_SESSION['your_time']))){

              }

              Hope this helps!

              This is exactly what i want to say
              More

                Write a Reply...