Friday, 31 August 2018

PHP strtotime () does not produce anything

Here is my PHP code:

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

That's what I get:
1. 0000-00-00 22:00:00
2.
3. 2:00

Why strtotime() outputs nothing by itself? Is there something wrong with server settings? Server: Apache/2.2.11 (Win32), PHP 5.2.10, MySQL client version: 5.0.51a.

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 ;-)

0 comments:

Post a Comment