(Unmoderated) manual notes are bad, mmkay?
Friday, October 30th, 2009I’ve had a couple of whinges on IRC lately about why I’m not thrilled with having user notes in their current form in the PHP manual; we get entirely too many questions in ##php from people who’ve copied code out of a note and are then annoyed when it turns out the code is wrong, broken, horrible, or all of the above.
I present this example from the DateTime::getTimestamp() manual page. It’ll be disappearing from the mirrors over the next few hours, because I’ve deleted it (and posted a much simpler note in its place), so here was its content, for posterity:
If you are using PHP < 5.3.0 you can use this function instead:
<?php function DateTime_getTimestamp(&$dt) { $dtz_original = $dt -> getTimezone(); $dtz_utc = new DateTimeZone("UTC"); $dt -> setTimezone($dtz_utc); $year = intval($dt -> format("Y")); $month = intval($dt -> format("n")); $day = intval($dt -> format("j")); $hour = intval($dt -> format("G")); $minute = intval($dt -> format("i")); $second = intval($dt -> format("s")); $dt -> setTimezone($dtz_original); return gmmktime($hour,$minute,$second,$month,$day,$year); } ?>
It’s fair to say that’s an interesting approach. The normal way of doing it would be:
<?php $timestamp = $dt->format('U'); ?>
I don’t know what the answer is — moderation has its own problems to do with workload, as PEAR can attest — but a system that’s letting that go up as recommended practice (and stay up for a month) has to be looked at.