如何为PHP格式化Bootstrap DatePicker [英] How to Format the Bootstrap DatePicker for 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>
,然后在我的PHP中捕获它:
and I capture that in my PHP here:
$date = $_POST['date'];
问题是DatePicker给我的格式是 dd / mm / yyyy
在我的$ date变量中 yyyy-mm-dd
时。我该如何重新格式化?
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?
推荐答案
******解决方案******
****** SOLUTION ******
为解决此问题,我看到变量'date'的格式为dd / mm / yyyy,并且为字符串。我能够解析它并以正确的格式将其重建为一个日期。
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);
*注意-我还必须将$ date变量用单引号引起来:
*Note - I also had to surround the $date variable in single quotes in the query:
$sql = "INSERT INTO tableName (..., date, ...)
VALUES (..., '".$date."', ...)";
这篇关于如何为PHP格式化Bootstrap DatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!