note 94466 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Fri, 06 Nov 2009 17:55:23 +0000 |
| Subject: | note 94466 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162461@lists.php.net to get a copy of this message | ||
Watch out when using strtotime() on a string that might begin with "0000-00-00" as MySQL
stores "null" dates on a not-null date column. The value returned by
strtotime("0000-00-00") varies by php version. Consider the following:
<?php
date('Y-m-d',strtotime('0000-00-00 00:00:00'));
// returns "1969-12-31" on the following versions:
// Win XP php 5.2.9-2
// Win 7 php 5.3.0
// RHEL 5 php 5.2.8
// but returns "-0001-11-30" on RHEL 5 php 5.2.10!
// consider checking for "zero" dates. e.g.:
function formatDate($format,$dateStr) {
if (trim($dateStr) == '' || substr($dateStr,0,10) == '0000-00-00') {
return '';
}
$ts = strtotime($dateStr);
if ($ts === false) {
return '';
}
return date($format,$ts);
}
?>
--was--
Watch out when using strtotime() on a string that might begin with "0000-00-00" as MySQL
stores "null" dates on a not-null date column. The value returned by
strtotime("0000-00-00") varies by php version. Consider the following:
<?php
date('Y-m-d',strtotime('0000-00-00 00:00:00'));
// returns "1969-12-31" on the following versions:
// Win XP php 5.2.9-2
// Win 7 php 5.3.0
// RHEL 5 php 5.2.8
// but returns "-0001-11-30" on RHEL 5 php 5.2.10!
// consider checking for "zero" dates. e.g.:
function formatDate($format,$dateStr) {
if (trim($dateStr) == '' || substr($dateStr,0,10) == '0000-00-00') {
return '';
}
$ts = strtotime($dateStr);
if ($ts === false) {
return '';
}
return date($format,$ts);
}
http://php.net/manual/en/function.strtotime.php