Calculate today



  • While working on a server migration I found this little gem:

    // calculate today
    $yesterday = mktime (0, 0, 0, date("m"),    date("d") - 1, date("Y"));
    $tomorrow =  mktime (0, 0, 0, date("m"),    date("d") + 1, date("Y"));



  • Wow... So nobody ever used this application on the first or last day of the month then?



  • You obviously didn't quote the rest:

    $today = date_avg($yesterday, $tomorrow);



  • I hate to be a spoilsport, but if it's anything like the C functions it will wrap out of range day values to the previous/next month/year. Jan 0 2008 = Dec 31 2007, etc. 



  • @Thief^ said:

    I hate to be a spoilsport, but if it's anything like the C functions it will wrap out of range day values to the previous/next month/year. Jan 0 2008 = Dec 31 2007, etc. 

    Seems like it: 

    mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. (Source



  • @PhillS said:

    Seems like it: 

    mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. (Source

     

    Comes in handy if you want to figure out the last of a any given month without having to whip up your own calendar function. Last day of February in the year XX is March 0, XX.



  •  I'm not sure what the problem here is supposed to be, other than that strtotime('yesterday') and strtotime('tomorrow') is a bit more clear and tiny bit faster.



  • Strtotime sure can be a blesing, but i'd hate to be the poor sucker who has to maintain that.
    I mean, think of the required algorithm! It takes text, numbers, numbers represented as text and more, all at once!



  • @MarcB said:

    @PhillS said:

    Seems like it: 

    mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. (Source

     

    Comes in handy if you want to figure out the last of a any given month without having to whip up your own calendar function. Last day of February in the year XX is March 0, XX.

    Might not be the best way, this will work and is what you would logically expect:

    [code] $numDaysInFeb = date('t', mktime(0,0,0,2,1,$year)); [/code]

    Simalar handy arguments are:
    w for day of week (0=sunday)
    z for day of year
    W for number of week
    L for leap year (little WTF: 0 and 1 instead of false and true, but PHP is the most weak-typed language I've ever seen)
    I if daylight saving time
    Also see: [url=http://www.php.net]The PHP manual on date()[/url]
    Can't say I really like PHP, but I think other languages could learn from its date/time functions. As is their easy-to-use, simple and fast online manual. I hate msdn.


  • @dtech said:

    As is their easy-to-use, simple and fast online manual.
     

    It is easy to use. I find it ultra-handy to be able to hit "php.net/whatever" and get redirected to the documentation on "whatever" automatically.

    Unfortunatley it's also too easy to comment on it. I'd figure that about 99.9% of the comments are useless "here's my c0dez for my super'leet method of adding two numbers!" type crap.

    Oh, and another example of good date/time functions is found in MySQL



  • As others have mentioned, this is a perfectly valid use of mktime();

    This leaves only two potential WTFs:

    1) The comment doesn't really match the code (although the variable names are appropriate)

    2) You posted this as a WTF without really understanding what the code does. 


Log in to reply