4

ここに私のPHPコードがあります:

echo '<br />1. '.$w_time_no;
echo '<br />2. '.strtotime($w_time_no);
echo '<br />3. '.date('G:i', strtotime($w_time_no));

それが私が得るものです:

1. 0000-00-00 22:00:00
2.
3. 2:00

なぜ strtotime() 自体は何も出力しないのですか? サーバーの設定がおかしくないですか?サーバー: Apache/2.2.11 (Win32)、PHP 5.2.10、MySQL クライアント バージョン: 5.0.51a。

4

3 に答える 3

11

strtotime doesn't "output" anything, btw : it returns false in case of an error ; see the manual :

Return Values

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

What doesn't output anything is echo : false is considered as an empty string, and nothing get outputed.

strtotime's documentation also gives the valid range for dates :

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (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. PHP 5.1.0 and newer versions overcome this limitation though.

'0000-00-00' is outside of this range, so it's not considered a valid date ; hence the false return value.


As a sidenote, to really know what's inside a variable, you can use var_dump.
As a bnus, used with Xdebug, it'll get you a nice-formated output ;-)

于 2009-08-03T21:23:44.153 に答える
7

0000-00-00 は有効な日付ではありません。

date() 入力時刻を 0 として解釈し、サーバーのタイムゾーンを補正するため、出力が得られます。date('Y-m-d H:i', strtotime(...))1970-01-01 2:00になると思います

于 2009-08-03T21:16:34.043 に答える
-2

You are mistaking strtotime() for time().

strtotime is literally string to time, it needs a string to convert..... to time.

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

So it is failing to validate that time string.

于 2009-08-03T21:23:09.240 に答える