日記
strtotime、dateのサポート範囲 (21:36)Edit

ある日付文字列を、$ndate = strtotime($sdate)でUNIXタイムスタンプに変換して内部で取り扱い、出力する際には$sdate = date('Y-m-d', $ndate)なんて感じで処理していたところ、1970-1-1より前の日付が軒並み1970-1-1になってしまった。

調べてみたところ、srttotime関数について手元(というか日本語版)のマニュアルの注意書きには、

注意: タイムスタンプの有効な範囲は、通常、Fri, 13 Dec 1901 20:45:54 GMTからTue, 19 Jan 2038 03:14:07 GMTまでです。(これらは、32ビッ ト符号付整数の最大及び最小に一致します。)

とだけしか書かれていなかったけど、英語版を見たら、

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.

と書かれていたよ。試してみたら、strtotime関数の段階ではきちんと(負の)UNIXタイムスタンプ値として変換されていた(RedHat/Fedora系Linux)。ただそれをdate関数で文字列に変換するときに、負の数値は0扱いされてしまい、1970-1-1になっちゃってるんだね。なんでstrtotimeできちんと負のタイムスタンプ値になっているのに、dateではそれを扱えないのか謎だ。っつーかこれどうやって解決しておくのがきれいなんだろうなー。今回はハイフン区切りの日付部のみだったから「-」でexplodeしちゃったけど、もっと汎用的な解決策を持っておきたいなー。

Published At2004-11-03 00:00Updated At2004-11-03 00:00