Javascript 如何将yy-mm-dd日期选择器帖子格式化为php?
我有一个基于JavaScriptjQueryUIDatePicker库的函数,用于选择从日期到日期的范围并发布到php。格式作为以下内容发布到php: 2015年5月13日星期三00:00:00 GMT+0200(中欧夏季时间) 如何在函数中设置datepicker post到php的格式(例如2015-05-20)Javascript 如何将yy-mm-dd日期选择器帖子格式化为php?,javascript,php,datepicker,Javascript,Php,Datepicker,我有一个基于JavaScriptjQueryUIDatePicker库的函数,用于选择从日期到日期的范围并发布到php。格式作为以下内容发布到php: 2015年5月13日星期三00:00:00 GMT+0200(中欧夏季时间) 如何在函数中设置datepicker post到php的格式(例如2015-05-20) function updateResults() { $.getJSON('results.php', { from_date: from
function updateResults() {
$.getJSON('results.php', {
from_date: fromDate.datepicker('getDate').toString(),
to_date: toDate.datepicker('getDate').toString()
}, function(data,status, xhr) {
$.plot('#flotcontainer', data, options);
});
}
//create a couple of date textboxes
$.datepicker.setDefaults
({
dateFormat: 'dd/mm/yy', defaultDate: '-1w', changeMonth: true, changeYear: true,
maxDate: 0, showButtonPanel: true, showWeek: true
});
var fromDate = $('#from').datepicker
({
onSelect: function()
{
var option = this.id == 'from' ? 'minDate': 'maxDate';
toDate.not(this).datepicker('option', option, $(this).datepicker('getDate'));
updateResults(); // When a new date is selected update the results
}
});
var toDate = $('#to').datepicker
({
onSelect: function()
{
updateResults(); // When a new date is selected update the results
}
});
//Set the default from and to dates.
fromDate.datepicker('setDate', '-1w');
toDate.datepicker('setDate', '+0');
updateResults(); // Initial call on load
});
格式作为以下内容发布到php:
2015年5月13日星期三00:00:00 GMT+0200(中欧夏季时间)
首先从中创建时间戳:
$ts = strtotime($_POST['date']);
如何在函数中将datepicker post的格式2015-05-20设置为php
function updateResults() {
$.getJSON('results.php', {
from_date: fromDate.datepicker('getDate').toString(),
to_date: toDate.datepicker('getDate').toString()
}, function(data,status, xhr) {
$.plot('#flotcontainer', data, options);
});
}
//create a couple of date textboxes
$.datepicker.setDefaults
({
dateFormat: 'dd/mm/yy', defaultDate: '-1w', changeMonth: true, changeYear: true,
maxDate: 0, showButtonPanel: true, showWeek: true
});
var fromDate = $('#from').datepicker
({
onSelect: function()
{
var option = this.id == 'from' ? 'minDate': 'maxDate';
toDate.not(this).datepicker('option', option, $(this).datepicker('getDate'));
updateResults(); // When a new date is selected update the results
}
});
var toDate = $('#to').datepicker
({
onSelect: function()
{
updateResults(); // When a new date is selected update the results
}
});
//Set the default from and to dates.
fromDate.datepicker('setDate', '-1w');
toDate.datepicker('setDate', '+0');
updateResults(); // Initial call on load
});
使用时间戳,您可以使用日期:
$date = date("Y-m-d", $ts);
您必须使用
$.datepicker.formatDate()代码>以转换日期
例如:
var date = $('.datepicker').datepicker('getDate');
var convertedDate = $.datepicker.formatDate('dd-mm-yy', date);
所以你的函数会变成
function updateResults() {
var from_date = fromDate.datepicker('getDate');
var to_date = toDate.datepicker('getDate');
from_date = $.datepicker.formatDate('yy-mm-dd', from_date);
to_date = $.datepicker.formatDate('yy-mm-dd', to_date);
$.getJSON('results.php', {
from_date: from_date,
to_date: to_date
}, function (data, status, xhr) {
$.plot('#flotcontainer', data, options);
});
}
要查看一个实例,请检查此
编辑:
使用$.datepicker.formatDate('yy-mm-dd',…)代码>以转换日期。我更改了答案,并提供了一个首先,您必须解释您正在使用的日期选择器库…在发布jquery ui日期选择器库之前,您可以使用javascript来处理。yy-mm-yy
?我觉得你有打字错误朱普,我也看到了并改变了它。谢谢你的解决方案不起作用。返回空白。那么('getDate').toString()呢?我不明白为什么它不工作。我添加了一个显示它工作的示例。编辑:它一直返回旧格式。我给你做了一个新的解决方案!我已经尝试过这样的php转换:$from_date=date('Y-m-d',strotime(preg_replace('/\s(.*$/','',$POST['from_date']));但当变量在mysql查询中时,它不起作用:$query=“SELECT date,info from datatbl WHERE date>=”$from_date”和date