Thread: Time()

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

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Time()


    I have the following code to go back 90 days.

    $start_date = time() - 90 * 3600 * 24;
    That goes back exactly 90 days to the second. Once I have the TIME string, how can I then get it converted back to the very first second of the day?

    For instance, run at 2:00pm, it goes back 90 days to 2:00pm. I need to take the $start_date then from there go back to 12:00:00am basically (the first second of the day in question.
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Doing date math with seconds is risky: unless you actually want to go back 7.7M seconds of real, elapsed time, daylight savings will give you unexpected results.
    PHP Code:
    $start_date strtotime("-90 days"); 
    Not to mention that's a lot easier to understand.

    Once you have the timestamp you can date() it - just use a literal string for the time component.
    PHP Code:
    $midnight date("Y-m-d"$start_date) . " 00:00:00"
    If you need another timestamp then strtotime() the above string.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by requinix
    Doing date math with seconds is risky: unless you actually want to go back 7.7M seconds of real, elapsed time, daylight savings will give you unexpected results.
    PHP Code:
    $start_date strtotime("-90 days"); 
    Not to mention that's a lot easier to understand.

    Once you have the timestamp you can date() it - just use a literal string for the time component.
    PHP Code:
    $midnight date("Y-m-d"$start_date) . " 00:00:00"
    If you need another timestamp then strtotime() the above string.
    I read somewhere mktime is deprecated which is why I didn't try it. Is that true?
  6. #4
  7. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Not in the least.

    But I didn't mention mktime()...?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Manchester
    Posts
    10
    Rep Power
    0
    As of PHP 5.1, when called with no arguments, mktime() throws an E_STRICT notice: use the time() function instead.
    As per php.net, it's not deprecated, just not advised. mktime() and time() will both return the current timestamp, time() just doesn't cause PHP to throw a ****y fit lol
  10. #6
  11. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Originally Posted by SWH Admin
    As per php.net, it's not deprecated, just not advised.
    Where do they say that?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Manchester
    Posts
    10
    Rep Power
    0
    Originally Posted by requinix
    Where do they say that?
    http://php.net/manual/en/function.mktime.php

    As of PHP 5.1, when called with no arguments, mktime() throws an E_STRICT notice: use the time() function instead.
  14. #8
  15. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Which says not to call the function without any arguments. Nothing about it being "not advised".
    Just making sure that's people reading your reply don't get the wrong idea.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Location
    Manchester
    Posts
    10
    Rep Power
    0
    I didn't say php.net doesn't advise it...I quoted what php.net says and then outside of the quote I was saying I wouldn't advise using mktime() if no arguments as PHP will throw a fit...

    Apologies for not being more clear

IMN logo majestic logo threadwatch logo seochat tools logo