PHP日期未插入格式正确

时间:2016-12-23 22:59:10

标签: php mysql wordpress

情况:

尝试运行查询以将userIDDate插入MySQL会导致插入用户ID并显示日期0000-00-00

代码:

$stdb->insert('sleep', array('userid'=>$userid,'date'=>date("Y-m-d")), array( '%s','%d' ));

是:

使用自定义WPDB类的Wordpress

我还有其他类似的查询(提交的数据越多越好),并且它们可以使用此代码功能。

我已将date("Y-m-d")写入日志记录,它出现了2016-12-23

错误记录不会返回错误,并且数据库已设置为MySQL列中的日期格式。

我能错过什么?

3 个答案:

答案 0 :(得分:0)

你有没有尝试过发送到unix时间?尝试更改:

foreach (var row in records)// about 150,000 record { string cs = row.Field<string>("tsb"); try { int rowCount = row.Table.Columns.Count - 6; string rowUserName = row.Field<string>("UserName").Trim(); if (rowUserName != userName) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_eror_user_unique + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_eror_user_unique + ":" + cs }); } long drh = Convert.ToInt32(row.Field<string>("drh")); var gm = db.gms.AsNoTracking().FirstOrDefault(g => g.number == drh && g.id_gmState == 1); if (gm == null || gm.rowCount != rowCount) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_eror_gm_count + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_eror_gm_count + ":" + cs }); } int ck = 0; int cj = 0; try { ck = Convert.ToInt32(row.Field<string>("_txtcodnakol")); cj = Convert.ToInt32(row.Field<string>("_txtcodnajoz")); } catch { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_erorr_code + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_erorr_code + ":" + cs }); } if (db.Cards.AsNoTracking().Any(c => c.id_user == user.Id && c.cs == cs && c.id_gm == gm.id && c.ck == ck && c.cj == cj)) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_eror_card_unique + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_eror_card_unique + ":" + cs }); } string value = ""; string rowContent = gm.gmRow.gmRowTitles.FirstOrDefault()?.rowContent; int countOption = Regex.Matches(rowContent, "ch=").Count; var coefficientValue = gm.CoefficientValues.Any() ? gm.CoefficientValues.FirstOrDefault().value : 1; int realCost = 0; for (int i = 1; i <= gm.rowCount; i++) // at least 15 rows { string option = row.Field<string>("j" + i); var distinctOption = option.Distinct().ToArray(); if (string.IsNullOrEmpty(option) || option.Length != distinctOption.Length) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs }); } for (int j = 0; j < distinctOption.Length; j++)// under 4 count loop { int item = Convert.ToInt32(distinctOption[j].ToString()); if (item < 1 || item > countOption) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_error_option_null + ":" + i + ":" + cs }); } } for (int j = 1; j <= countOption; j++) // under 4 count loop { if (option?.Contains(j + "") ?? false) value += "1,"; else value += "0,"; } if (realCost == 0 && i == 1) realCost = 1; realCost *= (option.Length * coefficientValue); } long cost = Convert.ToInt32(row.Field<string>("tmvs")); if ((realCost != cost && (realCost > gm.minCardValue)) || (realCost > gm.maxCardValue)) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs }); } if ((realCost != cost && (realCost <= gm.minCardValue))) { if (cost != gm.minCardValue) { task.taskStatus = (int)taskStatus.failed; task.details += Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = Languages.Properties.Resources.task_eror_card_cost_invalid + ":" + cs }); } } Card card = new Card { id_gm = gm.id, id_user = user.Id, id_cardStates = 1, value = value, Cost = cost, cj = cj, ck = ck, cs = cs }; db.Cards.Add(card); progress += perPercent; if ((int)progress > task.percentCompleted) { task.percentCompleted = (int)progress; TaskHelper.edit(task); } } catch (Exception ex) { task.taskStatus = (int)taskStatus.failed; task.details += ex.InnerException + ":" + cs; TaskHelper.edit(task); return Json(new { success = false, message = ex.InnerException + ":" + cs }); } } 'date'=>date("Y-m-d"))

假设您的数据库与您的php日期/时间同步,您可以使用date()

答案 1 :(得分:0)

我想出了问题,日期(“Y-m-d”)部分终止了第一个之后的日期 - 通过将其更改为日期(“Ymd”)我能够在数据库中正确地看到日期中继。

答案 2 :(得分:0)

要从php和mysql插入日期,您可以使用此代码。

<?php
date_default_timezone_set("Asia/Calcutta");
$date = date("d-m-y");

$sql = "INSERT INTO disp_time(userId, date) VALUES ('username','$date')";

if($link->query($sql)){
    echo "Query Successfully inserted";
}
else
{
    echo "Failed to insert".$sql."<br>".$link->error;
}
?>