0

如您所知,数据库中的日期格式是 Ymd,我需要在 dmY 中回显日期。这是回声的代码:

    <td class="pk">From</td>
          <td width="50%" class="pk_2"><input name="personal_ic_from" type="text" class="tcal" id="personal_ic_from" value="<?php
echo date_create($personal_ic_from)->format('d-m-Y');
?>" readonly="readonly"/>
          </td>

我的问题是,在用户更改日期后personal_ic_from,我需要在更新查询中将日期格式从 dmY 转换回 Ymd(这是数据库中日期的标准格式)。但是,我无法做到这一点..

这是我的更新查询:

<?php
$personal_ic_from = $_POST['personal_ic_from'];
$personal_ic_to  = $_POST['personal_ic_to'];
$personal_ic_num = $_POST['personal_ic_num'];
$personal_ic_old_num = $_POST['personal_ic_old_num'];
$personal_ic_issuing_autho = $_POST['personal_ic_issuing_autho'];
$personal_ic_issuing_num = $_POST['personal_ic_issuing_num'];
$personal_ic_issuing_date = $_POST['personal_ic_issuing_date'];
$personal_ic_date_expiry = $_POST['personal_ic_date_expiry'];
$personal_ic_place_issue = $_POST['personal_ic_place_issue'];
$personal_ic_country_issue = $_POST['personal_ic_country_issue'];
$personal_ic_country = $_POST['personal_ic_country'];


mysql_query("UPDATE personal_id_ic SET personal_ic_from = '".mysql_real_escape_string($_POST["personal_ic_from"])."', personal_ic_to = '".mysql_real_escape_string($_POST["personal_ic_to"])."', personal_ic_num = '".mysql_real_escape_string($_POST["personal_ic_num"])."', personal_ic_old_num = '".mysql_real_escape_string($_POST["personal_ic_old_num"])."', personal_ic_issuing_autho = '".mysql_real_escape_string($_POST["personal_ic_issuing_autho"])."', personal_ic_issuing_num = '".mysql_real_escape_string($_POST["personal_ic_issuing_num"])."', personal_ic_issuing_date = '".mysql_real_escape_string($_POST["personal_ic_issuing_date"])."', personal_ic_date_expiry = '".mysql_real_escape_string($_POST["personal_ic_date_expiry"])."', personal_ic_place_issue = '".mysql_real_escape_string($_POST["personal_ic_place_issue"])."', personal_ic_country_issue = '".mysql_real_escape_string($_POST["personal_ic_country_issue"])."', personal_ic_country = '".mysql_real_escape_string($_POST["personal_ic_country"])."' WHERE LAS_login_id = '".mysql_real_escape_string($_POST["LAS_login_id"])."'");

?>

如何将格式从 dmY 转换回 Ymd,以便数据库可以保存新日期?我尝试将查询更改为 for personal_id_ic,但是当我使用此代码时,数据库保存了一个随机日期,而不是用户选择的日期:

mysql_query("UPDATE personal_id_ic SET personal_ic_from = '".mysql_real_escape_string($_POST["personal_ic_from, %Y-%m-%d"])."' WHERE LAS_login_id = '".mysql_real_escape_string($_POST["LAS_login_id"])."'");
4

2 回答 2

2

用这个简单的短代码就可以轻松实现...

$dmy = "20-12-2013";
list($day, $month, $year) = explode("-", $dmy);
$ymd = "$year-$month-$day";
echo $ymd;
于 2013-10-01T01:15:16.893 回答
0

您可以使用 PHP 的strtotime函数将日期转换为 Unix 时间戳。从那里,您可以使用 PHP 的date函数以您想要的方式对其进行格式化。

一步:

$personal_ic_from = date('Y-m-d', strtotime($_POST['personal_ic_from']));

或分为2个步骤:

// Convert the form value to Unix timestamp
$unix_timestamp = strtotime($_POST['personal_ic_from']);

// Format the date how you want
$personal_ic_from = date('Y-m-d', $unix_timestamp);
于 2013-10-01T03:38:59.373 回答