2014-09-30 09:11
浏览 49

不能更改以PHP形式发布的日期格式,从mm / dd / yyyy到dd / mm / yyyy

I know this question has been asked before but none of the solutions is working for me, I have got a form in which the user enters the date of birth, but for reason it is set to mm/dd/yyyy and I want to change it to dd/mm/yyyy. As soon as i enter the date like 19/12/1992 it prompts invalid date format.

The code in the form is :

<label for="name">Date of Birth (mm/dd/yyyy) <font color="red"> *</font></label>
        <input type="text" name="DOB" value="<?php echo $info['DOB']; ?>" onfocus="$(this).removeClass('date required')" onchange="$(this).addClass('date required')" required />

And the code in the function after POST is:

$DOB = mysql_real_escape_string($_POST['DOB']);

图片转代码服务由CSDN问答提供 功能建议

我知道之前已经问过这个问题,但没有一个解决方案适合我,我有一个表格 用户输入的日期,但由于原因,它被设置为 mm / dd / yyyy ,我想将其更改为 dd / mm / yyyy 。 一旦我输入像19/12/1992这样的日期,它就会提示无效的日期格式。


 &lt; label for =“name”&gt;出生日期(mm / dd /  yyyy)&lt; font color =“red”&gt;  *&lt; / font&gt;&lt; / label&gt; 
&lt; input type =“text”name =“DOB”value =“&lt;?php echo $ info ['DOB'];?&gt;”  onfocus =“$(this).removeClass('date required')”onchange =“$(this).addClass('date required')”required /&gt; 


  $ DOB = mysql_real_escape_string($ _ POST ['DOB']); 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douwo3665 2014-09-30 09:34

    Like they all said do this

     $DOB = mysql_real_escape_string($_POST['DOB']);
     $newDate = date("d/m/Y", strtotime($DOB));

    Then at the other side where you are either inserting or updating the database change the date back to mysql standard format like this:

    $DOB_DB = mysql_real_escape_string($_POST['DOB']);
    $DOB_DB = date("m/d/Y", strtotime($DOB_DB));
    解决 无用
    打赏 举报
  • doulao1934 2014-09-30 09:17

    You could use strtotime:

    $oldDOB = mysql_real_escape_string($_POST['DOB']);
    $DOB = date("d/m/Y", strtotime($oldDOB));
    解决 无用
    打赏 举报
  • duanhan9479 2014-09-30 09:18

    EDITED: use this:


     $DOB = mysql_real_escape_string($_POST['DOB']);
               $newDate = date("d/m/Y", strtotime($DOB));


    2) $newDate = DateTime::createFromFormat('m/d/Y', $DOB)->format('d/m/Y');

    解决 无用
    打赏 举报

相关推荐 更多相似问题