Reputation: 15
I would like to edit current date of my posts which exists in database like; (type = datetime)
For instance #1;
2012-12-09 22:58:15
This is the post value (which I get successfully);
31/01/1980 08:20:00
When I use the following function to convert it to date I get '01/01/1970 01:00:00'
$date = date('d/m/Y H:i:s', strtotime($_POST['date']));
I really don't understand why it keeps giving error.
Thanks in advance.
Upvotes: 1
Views: 990
Reputation:
Try this according to posted answers:
<?php
$date = new DateTime(str_replace("/", "-", $_POST['date']));
echo $date->format('d/m/Y H:i:s');
?>
Upvotes: 0
Reputation: 71384
The issue you are having is likely related to this note from the PHP manual on strtotime:
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.
You are sending d/m/y and PHP is disambiguating this as an American m/d/y time because of the slash separator in the date.
Upvotes: 0
Reputation: 12806
From strtotime:
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.
You'll need to convert the date into 01/31/1980 08:20:00
or 31-01-1980 08:20:00
for strtotime to work correctly.
Upvotes: 0
Reputation: 360762
strtotime is failing, returning a boolean false, which is seen as (int)0 and translated to the unix epoch: midnight, jan 1, 1970
Read the nodes on the man page: http://php.net/strtotime for what formats it WILL accept/understand.
... then use http://php.net/date_create_from_format instead.
Upvotes: 4