开发者

php date format inserting wrong date

I am confused as to wh开发者_运维百科y this piece of code does not convert a date in the UK format 00/00/0000 to mysql date format 0000-00-00. What i get in the db is 1970-01-01. It is in the date format not datetime so it should work? Thanks

$destroy = mysql_real_escape_string($_POST['destroy']);
$desttempdate=str_replace("-", "/", $destroy); 
$destdate = date('Y-m-d', strtotime($desttempdate));


Here are a few options for you to get it working:

Something like this for PHP4:

function date2timestamp($date,$seperator='/'){
    if($date!=''){
        $dateEx = explode($seperator,$date);
        $date = (strlen($dateEx[2])==2?'20'.$dateEx[2]:$dateEx[2]).'-'.$dateEx[1].'-'.$dateEx[0].' 00:00:00';
    }
    return $date;
}

For PHP5.3 use createFromFormat():

$date = DateTime::createFromFormat('j/M/Y', $UK_date);
echo $date->format('Y-m-d');

Also other options for createFromFormat() in PHP5.2 include:

  • PHP createFromFormat for 5.2 version
  • php dateTime::createFromFormat in 5.2?
  • DateTime::createFromFormat in PHP < 5.3.0


strtotime() function will return Unix timestamp and 0000-00-00 date does not fit into that - you will just get 0 (zero) instead, which will be converted properly, to 1970-01-01.

You can do what you want by doing something like that:

$destdate = mysql_real_escape_string($_POST['destroy']); // assuming YYYY/MM/DD
$destdate = str_replace('/', '-', $destdate);
if ($destdate != '0000-00-00'){
    $destdate = date('Y-m-d', strtotime($desttempdate));
}


The problem is that you are passing to strtotime what it thinks is an invalid date. The documentation says:

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

So what's happening here is that you are passing in a string using the / separator, which means that strtotime tries to parse it as m/d/y instead of d/m/y. Either do not do the replace at all if your date is in the european format, or better yet use DateTime::createFromFormat() as the docs suggest.

You can check that this is indeed the problem by checking the return value of strtotime:

$destroy = mysql_real_escape_string($_POST['destroy']);
$desttempdate=str_replace("-", "/", $destroy);
var_dump(strtotime($desttempdate));

This will output "bool (false)", confirming that strtotime fails to parse its input.

Solution:

As mentioned above, prefer DateTime::createFromFormat():

$date = DateTime::createFromFormat('d/m/Y', $_POST['destroy']);
$destdate = $date->format('Y-m-d');

Otherwise, even though I do not recommend this approach, the simplest way to fix the problem would be simply to skip the str_replace:

$destdate = date('Y-m-d', strtotime($_POST['destroy']));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜