Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Code Explanation

    Hey guys,

    I have the code below and I need some help with it. I did not wrote this code so im not too sure how to edit it...

    PHP Code:
    $var = (time() - filemtime("dir/$file"))/60/60/240;
    if (
    $var 1unlink("dir/$file"); 
    Basically the code above deletes a file when it reaches a certain amount of days but I can't figure out when exactly it deletes the file. I want to know so I can either increase/decrease the amount of days when the file will be deleted, im assuming it has something to do with the "filemtime" function but I can't undertand the mathematics to it so if anyone know and willing to explain it to me that would be great.

    Any help will be greatly appreciated.

    Thanks

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It's using the unix timestamp, which is the number of seconds since January 1 1970 00:00:00 GMT. The time() function returns "right now", and the filemtime() function returns the timestamp on the file. That code is taking the difference between those two timestamps and dividing it by 60, giving the number of minutes between the two timestamps, then 60 again, giving the number of hours between the two timestamps, then by 240, giving "how many 240 hours" are between the two timestamps. If there are more than 240 hours between the two timestamps, then the value comes back greater than 1.

    So... basically... a 10 day old file (or greater) gets deleted.

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    close, but not quite.
    filemtime() returns the time of the last modification to the file, not the creation date.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Aha, but I was intentionally obtuse on that point-- I just said the "timestamp on the file"

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    It's using the unix timestamp, which is the number of seconds since January 1 1970 00:00:00 GMT. The time() function returns "right now", and the filemtime() function returns the timestamp on the file. That code is taking the difference between those two timestamps and dividing it by 60, giving the number of minutes between the two timestamps, then 60 again, giving the number of hours between the two timestamps, then by 240, giving "how many 240 hours" are between the two timestamps. If there are more than 240 hours between the two timestamps, then the value comes back greater than 1.

    So... basically... a 10 day old file (or greater) gets deleted.
    Thanks for the explantion, I sort of get it.. so if I want to delete the file in let say 5 days, what do I need to change, care to explain?

    Green rep added btw.

  • #6
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Quote Originally Posted by trancedm View Post
    Thanks for the explantion, I sort of get it.. so if I want to delete the file in let say 5 days, what do I need to change, care to explain?

    Green rep added btw.
    Change the 240 to 120 - ie the number of hours in 5 days

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aha! Thanks NancyJ!


  •  

    Posting Permissions

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