문제

In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .

$start_date = date('Y-m-d', $_POST['start_date']);

the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.

However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...

도움이 되었습니까?

해결책

Just do this:

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

다른 팁

You could also use strtotime() on your $_POST.

$start_date = date('Y-m-d', strtotime('05/12/2014'));

try to use

$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));

For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top