doqw89029
2015-05-27 22:20
浏览 63
已采纳

如何格式化PHP的Bootstrap DatePicker

I am using this Bootstrap DatePicker: code below

<input class="datepicker" name="date">

<script> //date picker js
  $(document).ready(function() {  
      $('.datepicker').datepicker({
         todayHighlight: true,
         "autoclose": true,
      });
  });   
</script>

and I capture that in my PHP here:

$date = $_POST['date'];

The problem is that the DatePicker gives me the format dd/mm/yyyy when I need it yyyy-mm-dd in my $date variable. How do I reformat this?

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

我正在使用 Bootstrap DatePicker :下面的代码

 &lt; input class =”datepicker“name =”date“&gt;  
 
&LT;脚本&GT;  //日期选择器js 
 $(文件).ready(function(){
 $('。datepicker')。datepicker({
 todayHighlight:true,
“autoclose”:true,
});  
});  
&lt; / script&gt; 
   
 
 

我在我的PHP中捕获了它:

  $ date = $  _POST ['date']; 
   
 
 

问题是DatePicker在需要时给我格式 dd / mm / yyyy 我的$ date变量中的 yyyy-mm-dd 。 如何重新格式化?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douzangdang2225 2015-05-28 14:51
    已采纳

    ****** SOLUTION ******

    To solve this issue I saw that the variable 'date' was formatted dd/mm/yyyy and was a string. I was able to parse it and rebuild as a date in the proper format.

    list($m,$d,$y) = explode("/",$_POST['date']);
    $timestamp = mktime(0,0,0,$m,$d,$y);
    $date = date("Y-m-d",$timestamp);
    

    *Note - I also had to surround the $date variable in single quotes in the query:

    $sql = "INSERT INTO tableName (..., date, ...)
            VALUES (..., '".$date."', ...)";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dthyxna3894 2015-05-27 22:23

    Try this page http://php.net/manual/en/datetime.createfromformat.php. Convert the string to a date object and output it as a formatted date

    You want Y-m-d.

    评论
    解决 无用
    打赏 举报
  • dongzg2006 2015-05-27 22:25

    I'm sure that you can set this in the date picker, but in PHP you can use:

    $date = DateTime::createFromFormat("d-m-Y", $_POST['date'])->format('Y-m-d');
    

    And from Bootstrap DatePicker documentation: http://bootstrap-datepicker.readthedocs.org/en/latest/options.html#format

    评论
    解决 无用
    打赏 举报
  • douzouchang7448 2015-05-29 14:16

    You can easily adapt https://stackoverflow.com/a/2487938/747609 to suit your need. Something along this line should solve your problem:

    $originalDate = $_POST['date'];
    $newDate = date("Y-m-d", strtotime($originalDate));
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题