#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0

    Adding time() to time() unexptected ressults


    Im getting 2 date variables . One is $dowrow["FAILTIME"] (pulling it out from mysql) the other is currentlFailingLength


    Code:
    $currentFailLength = date('H:i:s', (strtotime(date('H:i:s')) - strtotime($dowrow["LASTFAIL"])));
    gives 02:53:56

    $dowrow["FAILTIME"] is 00:00:04 and its TIME type in Mysql


    Code:
    $totalFailLength = date('H:i:s', strtotime($dowrow["FAILTIME"]) + strtotime($currentFailLength));
    echo $dowrow["FAILTIME"] ."+". $currentFailLength ."=". $totalFailLength;
    shows 00:00:04+02:53:56=17:25:44

    how to i get a proper answer in this example total should be 02:54:56 , right .... ?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    this doesn't make a lot of sense.

    First of all: What exactly are you trying to do? So you have a TIME column, and you wanna calculate the difference between the current time and your time in the database, right? How do you do that without a date information? What if the current time is 15:00, and the stored time is 16:00? Is that 1 hour? −1 hour? 23 hours? Something else?

    Secondly, I don't think you understand what those date functions do. For example, what is strtotime(date('H:i:s') supposed to do? And what about strtotime($currentFailLength)? I generally recommend staying away from low-level date calculations using Unix timestamps, because it's one of the things only few people seem to master. Use the DateTime class instead.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Im getting time variable from DB then calculating the second one what i want to do is to add those 2 together. like 00:10:00 + 00:00:59 = 00:10:59

    Code:
    $fail = $dowrow["FAILTIME"];
    $fail = new DateTime($fail);
    date_add($fail, date_interval_create_from_date_string($currentFailLength));
    echo date_format($fail, 'H:i:s');
    this code does not add $currentFailLength(2:00:00) to $fail(00:00:04) and the output is still 00:00:04
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    OK, I think you're confusing a lot of things here.

    First of all, you need to distinguish between a point of time (like "2 a.m.") and a time interval (like "3 hours and 20 minutes").

    In your calculation of $currentFailLength, you already got that wrong:

    PHP Code:
    $currentFailLength date('H:i:s', (strtotime(date('H:i:s')) - strtotime($dowrow["LASTFAIL"]))); 
    First you substract two Unix timestamps. That gives you a time interval in seconds. And then you pass those seconds to the date() in order to somehow get a point of time. How is that supposed to work? You cannot "convert" a time interval into an absolute point of time.

    The only reason why PHP doesn't crash is because the date() thinks you're giving it a Unix timestamp. If you pass 10 seconds, for example, it thinks you mean "1970-1-1 00:00:10". That's obviously nonsense.

    You also didn't answer my question: How do you subtract two times without a date information? What is "2:00 am − 3:00 am"? Is that 1 hour? −1 hour? 23 hours?

    This is the first problem you need to fix. And then we can go on with the other calculations.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    ROM
    Posts
    24
    Rep Power
    0

    Convert everything into seconds first


    Hi,

    I totally agree with Jacques1.

    When PHP works with time it uses only one thing seconds.

    You need to convert everything to seconds and then perform math operations and then get it convert it back to the format you want.

    Thanks.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    OK, I think you're confusing a lot of things here.

    First of all, you need to distinguish between a point of time (like "2 a.m.") and a time interval (like "3 hours and 20 minutes").

    In your calculation of $currentFailLength, you already got that wrong:

    PHP Code:
    $currentFailLength date('H:i:s', (strtotime(date('H:i:s')) - strtotime($dowrow["LASTFAIL"]))); 
    First you substract two Unix timestamps. That gives you a time interval in seconds. And then you pass those seconds to the date() in order to somehow get a point of time. How is that supposed to work? You cannot "convert" a time interval into an absolute point of time.

    The only reason why PHP doesn't crash is because the date() thinks you're giving it a Unix timestamp. If you pass 10 seconds, for example, it thinks you mean "1970-1-1 00:00:10". That's obviously nonsense.



    You also didn't answer my question: How do you subtract two times without a date information? What is "2:00 am − 3:00 am"? Is that 1 hour? −1 hour? 23 hours?
    2am - 3am cant happen because of the other parts of the code so the date part doesnt really matter to me though i will look in to it when i have the time

    Originally Posted by Jacques1
    This is the first problem you need to fix. And then we can go on with the other calculations.
    Thanks for the answer somehow managed to fix it myself

    Code:
    list($hours,$minutes,$seconds) = explode(":", $currentFailLength);
    $i = new DateInterval('PT'.$hours.'H'.$minutes.'M'.$seconds.'S');
    
    $fail = $dowrow["FAILTIME"];
    $fail = new DateTime($fail);
    				
    date_add($fail, $i);
    echo date_format($fail, 'H:i:s');
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    No, you did not fix it.

    I repeat one last time: $currentFailLength is wrong. It's not just the wrong value or something. It's conceptually wrong, because you're trying to convert a time interval into an absolute point of time. Your "H:i:s" string is an absolute point of time in the sense of "2 a.m.". It's not an interval in the sense of "H hours, i minutes and s seconds". That's what you might have in mind, but it's not what your code does.

    What you want is something like this:

    PHP Code:
    <?php

    // for testing
    $dowrow['LASTFAIL'] = '2:00';

    $last_fail = new DateTime($dowrow['LASTFAIL']);

    // subtract $last_fail from current time; this yields a time interval
    $now = new DateTime();
    $currentFailLength $now->diff($last_fail);
    And now you can take this time interval and add it to 'FAILTIME'.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    No, you did not fix it.

    I repeat one last time: $currentFailLength is wrong. It's not just the wrong value or something. It's conceptually wrong, because you're trying to convert a time interval into an absolute point of time. Your "H:i:s" string is an absolute point of time in the sense of "2 a.m.". It's not an interval in the sense of "H hours, i minutes and s seconds". That's what you might have in mind, but it's not what your code does.

    What you want is something like this:

    PHP Code:
    <?php

    // for testing
    $dowrow['LASTFAIL'] = '2:00';

    $last_fail = new DateTime($dowrow['LASTFAIL']);

    // subtract $last_fail from current time; this yields a time interval
    $now = new DateTime();
    $currentFailLength $now->diff($last_fail);
    And now you can take this time interval and add it to 'FAILTIME'.
    Seems i didnt explain the task properly. As you said LASTFAIL is a point and $fail is a point aswell when i substract one from another i get the interval betwen this 2 points , thats exactly what i need , considering i dont use or need days,weaks or years points range from 00:00 to 23:59 and the first one is always bigger. 1970-02-12 10:00 - 2013-09-13 05:00 will still give me 5 hours and thats why i need. I understand that is not the "proper" way to do it but im in a hurry and this gets the job done. Thanks for the support though.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    76
    Originally Posted by Stobor
    Seems i didnt explain the task properly. As you said LASTFAIL is a point and $fail is a point aswell when i substract one from another i get the interval betwen this 2 points , thats exactly what i need , considering i dont use or need days,weaks or years points range from 00:00 to 23:59 and the first one is always bigger. 1970-02-12 10:00 - 2013-09-13 05:00 will still give me 5 hours and thats why i need. I understand that is not the "proper" way to do it but im in a hurry and this gets the job done. Thanks for the support though.
    this remindes me of when a hacker hacked into the yahoo email database and stole all of the passwords that were stored as PLAIN TEXT

    the reason yahoo came up with for the unencrypted plain text passwords was because they were in a hurry while developing their system and never got around to updating the security and it was low on their todo list because what they had, got the job done



    now guess what resulted from this>?
    lots of lost clients and millions of $ lost due to houndreds of law suits, and being forced to pay even more $ due to negligence of security
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    The "I don't have time for that!" argument doesn't even make sense in this case, because the correct code would have been a lot shorter than fumbling with those conversion hacks (and coming up with excuses for doing it). Plus I wrote down the exact code.

    My boss once told me a joke about this:

    A walker meets a lumberjack, who has been trying all day to saw a little stub into pieces. Since the lumberjack doesn't seem to make any progress, the walker takes a closer look and realizes that the saw blade is completely blunt. So he tells the lumberjack and asks him why he doesn't change the blade. The lumberjack groans: "I don't have time for that! I need to get my work done!"

    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    The "I don't have time for that!" argument doesn't even make sense in this case, because the correct code would have been a lot shorter than fumbling with those conversion hacks (and coming up with excuses for doing it). Plus I wrote down the exact code.

    My boss once told me a joke about this:

    A walker meets a lumberjack, who has been trying all day to saw a little stub into pieces. Since the lumberjack doesn't seem to make any progress, the walker takes a closer look and realizes that the saw blade is completely blunt. So he tells the lumberjack and asks him why he doesn't change the blade. The lumberjack groans: "I don't have time for that! I need to get my work done!"

    Fixed the code with your help
    Code:
    $last_fail = new DateTime($dowrow['LASTFAIL']);
    $now = new DateTime();
    $i = new DateInterval('PT1H');
    date_add ($now , $i);
    						
    $total_fail_time = new DateTime($dowrow['FAILTIME']);
    $currentFailLength = $last_fail->diff($now);
    date_add ($total_fail_time, $currentFailLength);
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Agree


    Originally Posted by deveshnaswa
    Hi,

    I totally agree with Jacques1.

    When PHP works with time it uses only one thing seconds.

    You need to convert everything to seconds and then perform math operations and then get it convert it back to the format you want.

    Thanks.
    I toatally agree with what you are talking about .

IMN logo majestic logo threadwatch logo seochat tools logo