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