SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello, i've got a little php question
    I want an if statement that check the different between 2 times,
    $date1 = "19:01";
    $date2 = "20:35";
    if the different between $date1 and $date2 is >1hour (3600 sec), the statement has to be true, but how do I code that statement?
    The clue: I want to update a file every hour, when a user log in, the script will check how old the file is, is it older than 1 hour, the file has to be updated.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think should work, I didn't test it too much but seems to work fine.


    PHP Code:
    <?
        
    function get_utime($timestr) {
            
    $tmp explode(":"$timestr);
            return 
    mktime(0,$tmp[0],$tmp[1],date("m"),date("d"),date("y"));
            }

        
    $date1 get_utime("19:01");
        
    $date2 get_utime("20:07");
        if ((
    $date2 $date1) < 60) {
            print 
    "Less than a minute";    
            }
        else {
            print 
    "More than a minute";
            }
            
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This doesn't work
    The only difficult: compare to times (not the numbers).

    time: 23 59 < 00 01
    numbers: 23 59 > 00 01

    it's not easy

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean it doesn't work? Instead of saying it doesn't work, try telling me what doesn't work about it. It converts the two times into unix timestamps based off the minute and second. For your example

    $date1 turns into 984817141 after the function
    $date2 turns into 984817235 after the function

    if you subtract those two you get 94 seconds which is over 60 seconds right?

    So unless I misunderstood what you are seeking this works fine. Let's try with two times that are less than am inuet apart

    $date1 = get_utime("19:01");
    $date2 = get_utime("19:50");

    $date1 is now 984817141
    $date2 is now 984817190

    Subtract the two you get 49 seconds which is less than one minute.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now this script calculates for a different of 60 seconds (=1minute )
    But i need a script for 3600 seconds (=1hour).
    I tried this:
    PHP Code:
    <?
        
    function get_utime($timestr) {
            
    $tmp explode(":"$timestr);
            return 
    mktime(0,$tmp[0],$tmp[1],date("m"),date("d"),date("y"));
            }

            
    $date1 get_utime("19:01"); 
            
    $date2 get_utime("20:50"); 
        if ((
    $date2 $date1) < 3600) {
           print 
    "Less than a hour";    
            }
        else {
            print 
    "More than a hour";
            }
    ?>
    This returns to "Less than a hour", when i change $date2 to 19:20 it returns also to "Less than a hour"...
    This is my problem...

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is less than hour, right unless 19:01 refers to hours and minutes not minutes and seconds. Is this the case? If so

    make the function read:

    PHP Code:
        function get_utime($timestr) {
            
    $tmp explode(":"$timestr);
            return 
    mktime($tmp[0],$tmp[1],0,date("m"),date("d"),date("y"));
            } 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that's it!
    But i tried this:
    $date1 = "22:00";
    $date2 = "00:05";

    This gives a wrong output... the script gives "less than 1 hour", I suppose I have add day month year also in the script...

    how do I adapt the mktime function?
    how would you create the variable?
    $date1 = "03/18/2001-22:00"?

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will work:

    PHP Code:
    <?

        
    function get_utime($timestr) {
            
    $tmp explode("/"$timestr);
            
    $ttmp explode("-"$tmp[2]);
            
    $ttime explode(":"$ttmp[1]);
            return 
    mktime($ttime[0],$ttime[1],0,$tmp[0],$tmp[1],$ttmp[0]);
            }

            
    $date1 get_utime("03/18/2001-24:10"); 
            
    $date2 get_utime("03/19/2001-00:05"); 
        if ((
    $date2 $date1) < 3600) {
           print 
    "Less than a hour";    
            }
        else {
            print 
    "More than a hour";
            }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •