- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Just wonder what the code to find the frist day of a month would be....
Admin
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.
Admin
Why wouldn't he have solved this issue by simply doing this:
Admin
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.
Admin
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...
Admin
"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.
Admin
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...
Admin
[Obligatory comment about time on the DB server not being the same as the app server.]
Admin
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."
Admin
"<<<SQL" wth, whats that? Anyone got a php.net link for that? Thanks
Admin
Can't tell if trolling, wanting to be on tomorrow's TDWTF
( =_ =)
or simply ignorant...
Admin
Admin
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?
Admin
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... :(Admin
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.
Admin
http://php.net/manual/en/function.strtotime.php
Admin
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
Admin
So you mean strtotime("+1 day")?
Admin
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.)
Admin
The great thing about PHP is that you really need the "satire" identifier here to be unambiguous.
Admin
It appears that Chris M. is a spammer.
Excuse me while I don't give a damn about his plight.
Admin
Didn't know that
can be used in SQL and in PHP as well. Would it work in Java? I think I'll try...Admin
Admin
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Admin
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
Admin
Admin
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.
Admin
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.)
Admin
Depends on how you define "defined".
Admin
But sadly also:
Admin
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.
Admin
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.
Admin
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.
Admin
Admin
Bonus points if the application didn't actually need a database but he requested one so he could use it for date calculations.
Admin
Just another case of programmers not getting dates.
Admin
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.
Admin
TRWTF is dates again.
http://cdn.meme.li/instances/300x300/39757253.jpg
Admin
I think developers of this caliber don't really have "Swiss army knives" so much as "Swiss army hammers."
Admin
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.
Admin
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.
Admin
in the calendar of the real world, that's ±3600 every few months or so.
Admin
Admin
Fixed
Admin
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.
Admin
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!
Admin
Well, 2 reasons. For starters, this is a pretty round about way to do this even using MySQL. You can just do
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".)
Admin
I'm sure it's both.
Admin
Admin
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.