• (cs)

    Just wonder what the code to find the frist day of a month would be....

  • (cs)

    No wonder so many sites go down in flames with MySQL errors with some heavy traffic. Maybe PHP should have been a web layer on top of MySQL and not a whole web platform.

  • Tizian (unregistered)

    Why wouldn't he have solved this issue by simply doing this:

    for ($i = 0; $i <= 31; $i++) {
        $date = date('Y-m-d', time()+(86400*i));
        ...<snip>...
    }
    
  • (cs) in reply to Tizian

    You're well aware that this can give quite surprising results when it's (nearly) the middle of the night and daylight saving time kicks in (or out), sometime in the next 31 days?

    Although, to be fair, I think the same could happen with "+$i days" ... "CURDATE() + INTERVAL $i DAYS" at least reads sane, but of course it could be buggy, too.

  • Eric TF Bat (unregistered) in reply to Tizian

    I worked on a project once that used date functions and failed, but only in some (Australian) states. It was written and thoroughly tested in Queensland, but failed in New South Wales and Victoria. Turns out there are times when the number of seconds from midnight to midnight is not exactly 24x60x60, due to daylight savings -- but Queensland doesn't suffer from that because the farmers don't want to confuse their cows...

  • Nick (unregistered)

    "The other developer offered that perhaps the code's author had understood SQL better than PHP. While Chris appreciated the effort, the thirty-two INSERTs into the same field in the same table made that justification seem unlikely."

    As someone who does understand SQL a lot better than PHP, I don't get why Chris would see that justification as unlikely. I probably would have written something very similar myself! (although I'd like to think I would have put all VALUES into one INSERT...)

    But then, I'm not being paid to write PHP code.

  • anonymous (unregistered)

    I'm starting to wonder if I hate working with dates simply because they ARE a pain, or because more often than not we find code that makes it twice the pain...

  • Jeve Stobs (unregistered)

    [Obligatory comment about time on the DB server not being the same as the app server.]

  • MiniMax (unregistered)

    It's about more than dates and languages. It's about being humble. It's about thinking "Ohh, I have this problem, and I am sure I'm not the center of the universe, so it's a good bet that someone else have had a similar problem in the past. So why don't I go see if I can't find an existing solution to my problem instead of trying to be clever and do it all on my own."

  • olaf (unregistered)

    "<<<SQL" wth, whats that? Anyone got a php.net link for that? Thanks

  • 50% Opacity (unregistered) in reply to Tizian
    Tizian:
    Why wouldn't he have solved this issue by simply doing this:
    for ($i = 0; $i <= 31; $i++) {
        $date = date('Y-m-d', time()+(86400*i));
        ...<snip>...
    }
    

    Can't tell if trolling, wanting to be on tomorrow's TDWTF

    ( =_ =)

    or simply ignorant...

  • daveyk (unregistered) in reply to Eric TF Bat
    Eric TF Bat:
    but Queensland doesn't suffer from that because the farmers don't want to confuse their cows...
    It is actually because if we introduce daylight savings then the curtains will fade.
  • Franky (unregistered)

    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days? I mean, seriously, is there no DateTime.Today.AddDays(i) in php?

  • 50% Opacity (unregistered) in reply to Franky
    Franky:
    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days? I mean, seriously, is there no DateTime.Today.AddDays(i) in php?

    Well, PHP is not an OO language and does not have a primitive for dates, so it's mostly working with UNIX timestamp integers. Havin said that, it does have the DateTime class which is becoming halfway decent recently, it's just that most PHP developers still think a day has 60 * 60 * 24 seconds... :(

  • (cs)

    re: Daylight Saving Time...

    Here at WTF Inc, our code is heavily dependent upon date calculations. Dealing with transactions that occur in the middle of a DST switch would greatly complicate the home grown libraries we have. As such, the authors instituted, and management approved a procedural rule that suspends all processing for two hours on either side of a DST change.

  • Erich (unregistered) in reply to Franky
    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days? I mean, seriously, is there no DateTime.Today.AddDays(i) in php?

    strtotime("+1 day", time());

    http://php.net/manual/en/function.strtotime.php

  • (cs) in reply to olaf
    olaf:
    "<<<SQL" wth, whats that? Anyone got a php.net link for that? Thanks</div>

    it is called herdoc format & is actually quite usefully if you have multi-line strings especially if they contain a mix of double & single quotes

    that does not make it an less of a WTF though

  • 50% Opacity (unregistered) in reply to Erich
    Erich:
    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days? I mean, seriously, is there no DateTime.Today.AddDays(i) in php?

    strtotime("+1 day", time());

    http://php.net/manual/en/function.strtotime.php

    So you mean strtotime("+1 day")?

  • Bill P. Godfrey (unregistered)

    Remember, to compare two date/time values to within the same day, use the == operator. To compare within the same second, use ===. To compare down to the microsecond you need the ==== operator.

    (Satire.)

  • Daniel (unregistered) in reply to Bill P. Godfrey

    The great thing about PHP is that you really need the "satire" identifier here to be unambiguous.

  • Florent (unregistered)

    It appears that Chris M. is a spammer.

    Excuse me while I don't give a damn about his plight.

  • ZoomST (unregistered)

    Didn't know that

    ...<snip>...
    can be used in SQL and in PHP as well. Would it work in Java? I think I'll try...

  • ZoomST (unregistered) in reply to Florent
    Florent:
    It appears that Chris M. is a spammer.

    Excuse me while I don't give a damn about his plight.

    Nope. Is working for a spam company, that is different. And probably is being forced to work like Hugh Jackman in that film, Swordfish.

  • Bananafana (unregistered) in reply to olaf

    http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

  • phord (unregistered) in reply to Franky

    I love the gnu date parsers.

    $ date --date "2 fortnights from last Tuesday"

    http://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings

  • Tinus (unregistered) in reply to 50% Opacity
    50% Opacity:
    it's just that most PHP developers still think a day has 60 * 60 * 24 seconds... :(
    A SI day is defined as 86400 seconds.
  • rrr (unregistered)

    Yay for PHP trolling day. OK I'll bite:

    http://uk1.php.net/datetime

    which is much better than the previous strtotime bits and bobs, handles timezones properly, allows date addition easily etc etc.

  • (cs)
    Tinus:
    A SI day is defined as 86400 seconds.

    There was a "leap second" a few years back.

    Guess what, if SI doesn't match what people do in reality, it is useless for the purposes of tracking reality.

    (I don't know if it does or doesn't in this particular case, just speaking in general here.)

  • 50% Opacity (unregistered) in reply to Tinus
    Tinus:
    50% Opacity:
    it's just that most PHP developers still think a day has 60 * 60 * 24 seconds... :(
    A SI day is defined as 86400 seconds.

    Depends on how you define "defined".

    The unit of measurement for time called "day", redefined in 1967 as 86,400 SI seconds and symbolized d, is not an SI unit, but it is accepted for use with SI. A civil day is usually also 86,400 seconds, plus or minus a possible leap second in Coordinated Universal Time UTC, and, in some locations, occasionally plus or minus an hour when changing from or to daylight saving time.

    http://en.wikipedia.org/wiki/Day

  • (cs) in reply to phord
    phord:
    I love the gnu date parsers.

    $ date --date "2 fortnights from last Tuesday"

    http://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings

    Version 8.13 of coreutils doesn't handle that particular string, but does handle things like:

    $ date --date "2 fortnights" 
    Tue Aug 13 14:31:54 CEST 2013
    $ date --date "2 fortnights ago" 
    Tue Jun 18 14:31:50 CEST 2013

    But sadly also:

    $ date --date "2 fortnight" 
    Tue Aug 13 14:32:59 CEST 2013
  • zennnnnn (unregistered)

    The is a not entirely unreasonable possible explanation for this.

    Mixing time and date function across two system may be problematic if they differ at all. Not that all the functions appear to work around CURDATE.

    Many setups will have one mysql server and multiple webservers. For time consistency it makes sense in those situations to use the database for time.

    PHP has date/time functions but they are not great and performing operations across PHP/MYSQL is not pretty.

    I can't understand exactly what is meant to be accomplished in this code sample, but there may be some very good reasons for it.

  • jon (unregistered) in reply to Franky
    Franky:
    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days? I mean, seriously, is there no DateTime.Today.AddDays(i) in php?

    The behavior of strtotime is well defined.

    Yes, there is this: $date = new DateTime(); $date->add(new DateInterval("P1D"));

    and the format of the string expected by DateInterval is also well defined.

    But there's nothing inherently wrong with the strtotime syntax. Its syntax is undefined, not vague voodoo magic.

  • (cs) in reply to Eric TF Bat
    Eric TF Bat:
    due to daylight savings -- but Queensland doesn't suffer from that

    Well TDWTF suffers from incorrect daylight savings time!

    Posted 11:24PM Queensland time.

    Edit: OK here it says "09:24" but in the forums it's only one hour out during winter, correct during summer.

  • (cs) in reply to daveyk
    daveyk:
    Eric TF Bat:
    but Queensland doesn't suffer from that because the farmers don't want to confuse their cows...
    It is actually because if we introduce daylight savings then the curtains will fade.
    It's actually because the populous south east of the state is geographically in the wrong timezone, meaning it is permanently in daylight savings mode, and the north of the state is tropical so there is little difference between summer and winter, apart from the weight of the constant rain.
  • (cs)

    Bonus points if the application didn't actually need a database but he requested one so he could use it for date calculations.

  • D8 (unregistered)

    Just another case of programmers not getting dates.

  • RakerF1 (unregistered) in reply to Jeve Stobs
    Jeve Stobs:
    [Obligatory comment about time on the DB server not being the same as the app server.]

    Which is why you run a service or script to keep them synchronized with the DB Server configured as the source of record, which is synchronized with something like NIST.

  • Anom nom nom (unregistered) in reply to D8
    D8:
    Just another case of programmers not getting dates.

    TRWTF is dates again.

    http://cdn.meme.li/instances/300x300/39757253.jpg

  • erat (unregistered)

    I think developers of this caliber don't really have "Swiss army knives" so much as "Swiss army hammers."

  • JAPH (unregistered) in reply to Tizian
    Tizian:
    Why wouldn't he have solved this issue by simply doing this:
    for ($i = 0; $i <= 31; $i++) {
        $date = date('Y-m-d', time()+(86400*i));
        ...<snip>...
    }
    

    86400 doesn't take into account leap seconds or daylight savings time. Also, the value of time() should probably stored outside the loop in the case that the date changes during the iteration.

  • (cs) in reply to 50% Opacity
    50% Opacity:
    Tinus:
    50% Opacity:
    it's just that most PHP developers still think a day has 60 * 60 * 24 seconds... :(
    A SI day is defined as 86400 seconds.

    Depends on how you define "defined".

    The unit of measurement for time called "day", redefined in 1967 as 86,400 SI seconds and symbolized d, is not an SI unit, but it is accepted for use with SI. A civil day is usually also 86,400 seconds, plus or minus a possible leap second in Coordinated Universal Time UTC, and, in some locations, occasionally plus or minus an hour when changing from or to daylight saving time.

    http://en.wikipedia.org/wiki/Day

    SI is awesome, until it reaches something that can't be SIed. Then, not so awesome.

    Besides, if a day were SIed. It'd be 10010010, not 606024. If we went with 10010010, today's second would be shorter at .864 of the new SI second.

    But then, our leap seconds wouldn't be right.

    But just imagine, conversion would be a snap.

    23,923 seconds would be 2 hr, 39 min, 23 seconds.

  • drake (unregistered) in reply to Tinus

    in the calendar of the real world, that's ±3600 every few months or so.

  • hank (unregistered) in reply to Franky
    Franky:
    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days?

    Yes.

  • (cs) in reply to JAPH
    JAPH:
    Tizian:
    Why wouldn't he have solved this issue by simply doing this:
    for ($i = 0; $i <= 31; $i++) {
        $date = date('Y-m-d', time()+(86400*i));
        ...<snip>...
    }
    

    86400 doesn't take into account leap seconds or daylight savings time. Also, the value of time() should probably stored outside the loop in the case that the amount of seconds that time() returns changes during the iteration.

    Fixed

  • (cs) in reply to Franky
    Franky:
    am I the only one who looks at

    date('Y-m-d', strtotime("+$i days"))

    and gets blood in the eyes regarding "+$i days" parsing to today in x days? I mean, seriously, is there no DateTime.Today.AddDays(i) in php?

    Oh man, just wait until you see the string format methods in C++/C#/other languages.

    "%0d ounces of oil"

    I mean, parsing strings to build strings. It's inane.

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered)

    You can always get an idea how bad a programmer is by looking at how he implements date functions.

    Sometimes just the simple fact that he even wrote them at all in the first place is enough evidence.

    "Date functions in PHP" by H.P. Lovecraft, available now from Amazon!

  • Jeremy (unregistered) in reply to Nick
    Nick:
    "The other developer offered that perhaps the code's author had understood SQL better than PHP. While Chris appreciated the effort, the thirty-two INSERTs into the same field in the same table made that justification seem unlikely."

    As someone who does understand SQL a lot better than PHP, I don't get why Chris would see that justification as unlikely. I probably would have written something very similar myself! (although I'd like to think I would have put all VALUES into one INSERT...)

    But then, I'm not being paid to write PHP code.

    Well, 2 reasons. For starters, this is a pretty round about way to do this even using MySQL. You can just do

    SELECT (DATE_ADD(CURDATE(), INTERVAL+12 DAY))
    and not involve a table.

    Secondly, it falls into the same trap that half these TDWTFs do. Namely that the developer isn't aware of their own limitations, or are unable to identify when something is needlessly complicated. It should be pretty obvious to anyone doing this that it's not "the answer". PHP sucks, sure, but even with that surely this person can't think that anytime we want "today + 2" we make a date table in mysql, insert a row, and then get it back out. Any programmer should be able to identify which problems are indeed some unique and bizarre requirement vs which ones are really common. (And, as a parallel, when you do google for the answer, which ones are the "blind leading the blind" still needlessly complicated ones, vs the "real one".)

  • ping floyd (unregistered) in reply to anonymous
    anonymous:
    I'm starting to wonder if I hate working with dates simply because they ARE a pain, or because more often than not we find code that makes it twice the pain...

    I'm sure it's both.

  • Neil (unregistered) in reply to olaf
    olaf:
    "<<<SQL" wth, whats that? Anyone got a php.net link for that? Thanks</div>
    Here, let me help: http://php.net/manual/en/language.types.string.php
  • Michael (unregistered) in reply to JAPH
    JAPH:
    Tizian:
    Why wouldn't he have solved this issue by simply doing this:
    for ($i = 0; $i <= 31; $i++) {
        $date = date('Y-m-d', time()+(86400*i));
        ...<snip>...
    }
    

    86400 doesn't take into account leap seconds or daylight savings time. Also, the value of time() should probably stored outside the loop in the case that the date changes during the iteration.

    time() returns the number of seconds since the epoch in UTC, so daylight saving time is not a problem. Leap seconds could be a problem occasionally.

Leave a comment on “PHP Doesn't Have Date Functions Either”

Log In or post as a guest

Replying to comment #:

« Return to Article