twitter-bootstrap 如何为 PHP 格式化 Bootstrap DatePicker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30494086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Format the Bootstrap DatePicker for PHP
提问by Nate May
I am using this Bootstrap DatePicker: code below
我正在使用这个Bootstrap DatePicker:下面的代码
<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:
我在我的 PHP 中捕获了它:
$date = $_POST['date'];
The problem is that the DatePicker gives me the format dd/mm/yyyywhen I need it yyyy-mm-ddin my $date variable. How do I reformat this?
问题是 DatePicker在 $date 变量中dd/mm/yyyy需要时为我提供格式yyyy-mm-dd。我该如何重新格式化?
采纳答案by Nate May
****** 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.
为了解决这个问题,我看到变量“日期”的格式为 dd/mm/yyyy 并且是一个字符串。我能够解析它并以正确的格式重建为日期。
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:
*注意 - 我还必须在查询中用单引号将 $date 变量括起来:
$sql = "INSERT INTO tableName (..., date, ...)
VALUES (..., '".$date."', ...)";
回答by Ron Dadon
I'm sure that you can set this in the date picker, but in PHP you can use:
我确定您可以在日期选择器中进行设置,但在 PHP 中您可以使用:
$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
来自 Bootstrap DatePicker 文档:http: //bootstrap-datepicker.readthedocs.org/en/latest/options.html#format
回答by IROEGBU
You can easily adapt https://stackoverflow.com/a/2487938/747609to suit your need. Something along this line should solve your problem:
您可以轻松调整https://stackoverflow.com/a/2487938/747609以满足您的需要。沿着这条线的东西应该可以解决你的问题:
$originalDate = $_POST['date'];
$newDate = date("Y-m-d", strtotime($originalDate));
回答by Connor Wyatt
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
试试这个页面http://php.net/manual/en/datetime.createfromformat.php。将字符串转换为日期对象并输出为格式化日期
You want Y-m-d.
你想要 Ymd。

