开发者

Re-Convert timestamp DATE back to original format (when editing) PHP MySQL

Ok so I have managed to get the format of the date presented in HTML (upon display) to how I want (dd/mm/yyy)...However, the user also can change the date via a form.

So this is how it's setup as present. Firstly, the conversion from YYYY-MM-DD to DD/MM/YYYY and then display in HTML:

$timestamp = strtotime($duedate); echo date('d/m/Y', $timestamp);

Now when the user selects to update the due date, the va开发者_Python百科lue already stored value is drawn up and presented in my text field (using exactly the same code as above).. All good so far.

But when my user runs the update script (clicks submit), the new due date is not being stored, and in my DB its seen as '0000-00-00'. I know I need to convert it back to the correct format that MySQL wants to have it in, in order for it to be saved, but I'm not sure on how to do this. This is what I have so far in my update script (for the due date):

$duedate = $_POST['duedate'];

$timestamp = strtotime($duedate);
    date('Y/m/d', $timestamp);

$query = "UPDATE films SET filmname= '".$filmname."', duedate= '".$duedate;

Can somebody please point me in the right direction to have the new date, when processed, converted back to the accepted format by MySQL. I appreciate any help given! Thanks.


This should help. Try doing the same conversion, but back to the format MySQL needs. Look at the date() function for formats.

 $duedate = date('Y-m-d', strtotime($_POST['duedate']));
 // Alternate, more accurate version.
 $date_part = explode("/", $_POST['duedate']);
 $duedate = date('Y-m-d', mktime(0, 0, 0, $date_part[1], $date_part[0], $date_part[2]));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜