我使用带有以下代码的jQuery datepicker
$(".item").datepicker({
showWeek: true,
firstDay: 1,
minDate: -30,
dateFormat: 'mm-dd-yy'
});
该datepicker的值将通过ajax发送到php函数,该函数在mysql表中插入数据。
mysql数据库中的date
列类型为Datetime
。
每当从datepicker输入值读取值时,数据库中的date
列为空,显示00-00-0000 00:00:00
。
我是php的新手,也许我犯了一个错误。
php代码
mysqli_query($connect, "Insert into tab(date) values ('".$myData."')");
如何为mysql格式化javascript日期?
答案 0 :(得分:4)
您是否可以首先验证日期选择器是否向服务器发布了正确的值?
尝试在某处提醒值。
如果你有正确的javascript输入,那么脚本的php部分就可以这样完成:
if (isset$_GET['date']){$date=$_GET['date'];}
$date=date("Y-m-d h:i:s",strtotime($date));
回声确认你没错,最后将$ date插入表格。
答案 1 :(得分:1)
mysql中的日期格式为YYYY-MM-DD,因此您可以使用strtotime:
$myDataSQL = date("Y-m-d", strtotime($myData));
mysqli_query($connect, "Insert into tab(date) values ('".$myDataSQL."')");
顺便说一句,我建议使用预准备语句来避免sql注入问题,尽管在这种特定情况下它并不重要。我总是使用准备好的陈述,所以我不必考虑它。
修改:strtotime needs /
separators似乎有效。
如果您使用的是PHP 5.3+,则可以使用DateTime
类:
$date = DateTime::createFromFormat('m-d-y', $myData);
$myDataSQL = $date->format('Y-m-d');
mysqli_query($connect, "Insert into tab(date) values ('".$myDataSQL."')");
答案 2 :(得分:0)
queryMccording to mysql documentation:
作为“YYYY-MM-DD”或“YY-MM-DD”格式的字符串。允许使用“宽松”语法:任何标点符号都可以用作日期部分之间的分隔符。例如,'2012-12-31','2012/12/31','2012 ^ 12 ^ 31'和'2012 @ 12 @ 31'是等价的。
所以你可以改变datepicker采用日期的格式。
$( ".item" ).datepicker({ dateFormat: "yy-mm-dd" });
将它放入您的更新应该可以解决问题,您甚至不需要额外的服务器操作来进行解析。只需确保它能够安全到达服务器。
答案 3 :(得分:0)
对jeroen的答案有不同看法,这个答案也是合适的
$myDataSQL = date("c", strtotime($myData));
mysqli_query($connect, "Insert into tab(date) values ('".$myDataSQL."')");
我发现使用ISO 8601日期更容易处理时区,因为它们是可见的。另外它插入到mysql中。
同样在过去,我遇到了jQuery datapicker的问题,需要在初始化时指定更好的格式。
也许试试
$(".item").datepicker({
showWeek: true,
firstDay: 1,
minDate: -30,
dateFormat: "yy/mm/dd"
});
答案 4 :(得分:0)
我使用转换器(源代码:https://github.com/lingtalfi/DatePickerHelper/blob/master/DatePickerHelper.php)
用法:
<?php
echo DatePickerHelper::convertFromDatePickerToPhpDate("dd/mm/yy"); // prints d/m/Y
echo DatePickerHelper::convertFromPhpDateToDatePicker("Y-m-d"); // prints yy-mm-dd
$birthdayDate = "09/12/1944"; // your input from the web
echo DatePickerHelper::convertFromNumericInputToMysqlDate($birthdayDate, "d/m/Y"); // 1944-12-09
-
class DatePickerHelper
{
private static $map = [
"yy" => "<1>",
"y" => "<2>",
"MM" => "<3>",
"M" => "<4>",
"mm" => "<5>",
"m" => "<6>",
"DD" => "<7>",
"D" => "<8>",
"oo" => "<9>",
"o" => "<10>",
"dd" => "<11>",
"d" => "<12>",
];
private static $map2 = [
"<1>" => 'Y',
"<2>" => 'y',
"<3>" => 'F',
"<4>" => 'M',
"<5>" => 'm',
"<6>" => 'n',
"<7>" => 'l',
"<8>" => 'D',
"<9>" => 'z', // note: php doesn't have "day of the year with three digits", but this is the closest
"<10>" => 'z',
"<11>" => 'd',
"<12>" => 'j',
];
private static $mapRegex = [
'Y' => '<1>',
'y' => '<2>',
'm' => '<3>',
'n' => '<4>',
'd' => '<5>',
'j' => '<6>',
];
private static $mapRegex2 = [
'<1>' => '(?P<year4>[0-9]{4})',
'<2>' => '(?P<year2>[0-9]{2})',
'<3>' => '(?P<month_leading>[0-9]{2})',
'<4>' => '(?P<month_no_leading>[0-9]{1,2})',
'<5>' => '(?P<day_leading>[0-9]{2})',
'<6>' => '(?P<day_no_leading>[0-9]{1,2})',
];
public static function convertFromDatePickerToPhpDate(string $datePickerFormat): string
{
$map = self::$map;
$map2 = self::$map2;
$first = str_replace(array_keys($map), array_values($map), $datePickerFormat);
return str_replace(array_keys($map2), array_values($map2), $first);
}
public static function convertFromPhpDateToDatePicker(string $phpDate): string
{
$map2 = array_flip(self::$map2);
$map = array_flip(self::$map);
$first = str_replace(array_keys($map2), array_values($map2), $phpDate);
return str_replace(array_keys($map), array_values($map), $first);
}
/**
* @param string $input , the string to convert, the format of this string should match the given phpFormat
* Plus, it must contain exactly:
* - one day component
* - one month component
* - one year component
*
* @param string $phpFormat , all components of the phpFormat have to be one of those:
* - Y: year, four digits
* - y: year, two digits
* - m: numeric month, with leading zeros
* - n: numeric month, without leading zeros
* - d: numeric day of the month, with leading zeros
* - j: numeric day of the month, without leading zeros
*/
public static function convertFromNumericInputToMysqlDate(string $input, string $phpFormat)
{
$map = self::$mapRegex;
$map2 = self::$mapRegex2;
$first = str_replace(array_keys($map), array_values($map), $phpFormat);
$pattern = str_replace(array_keys($map2), array_values($map2), $first);
if (preg_match('!' . $pattern . '!', $input, $match)) {
$day = $match['day_leading'] ?? $match['day_no_leading'] ?? null;
if (null !== $day) {
$day = (int)$day;
$month = $match['month_leading'] ?? $match['month_no_leading'] ?? null;
if (null !== $month) {
if (
array_key_exists("year4", $match) ||
array_key_exists("year2", $match)
) {
// a component of each type is there, we will be able to return a result
if (array_key_exists("year4", $match)) {
$year = (int)$match['year4'];
} else {
// assumed it's 20, but we don't know really, that sucks.
// That's why you should use year4 instead...
$year = "20" . $match['year2'];
$year = (int)$year;
}
return $year . "-" . sprintf('%02s', $month) . "-" . sprintf("%02s", $day);
}
}
}
}
return false;
}
}
答案 5 :(得分:0)
在JavaScript中,日期值是使用private void getUserProfile(AccessToken currentAccessToken) {
GraphRequest request = GraphRequest.newMeRequest(
currentAccessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("TAG", object.toString());
try {
String first_name = object.getString("first_name");
String last_name = object.getString("last_name");
String email = object.getString("email");
String id = object.getString("id");
String image_url = "https://graph.facebook.com/" + id + "/picture?type=normal";
appData.setEmail(email);
appData.setUsername(first_name + " " + last_name);
appData.setGoogleID(id);
appData.setProfile(image_url);
loginPresenter.signInThroughSocialMedia(first_name + " " + last_name, email, id);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "first_name,last_name,email,id");
request.setParameters(parameters);
request.executeAsync();
}
创建的,因此在PHP脚本中格式化日期时必须使用相同的格式。
只需将您的php日期转换为以下格式let date = new Date("2017-01-26");
。
这将为javascript date('Y-m-d', $date)
字段提供正确的格式。