将日期格式从d-m-Y转换为Y-m-d

时间:2013-10-01 01:07:09

标签: php mysql sql date-format wampserver

如您所知,数据库中的日期格式为Y-m-d,我需要在d-m-Y中回显日期。 这是echo的代码:

    <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的日期后,我需要在更新查询中将日期格式从d-m-Y转换回Y-m-d(这是数据库中日期的标准格式)。但是,我无法做到这一点..

这是我的更新查询:

<?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"])."'");

?>

如何将格式从d-m-Y转换回Y-m-d,以便数据库可以保存新日期? 我尝试将查询更改为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"])."'");

2 个答案:

答案 0 :(得分:2)

使用这个简单的短代码很容易实现...

$dmy = "20-12-2013";
list($day, $month, $year) = explode("-", $dmy);
$ymd = "$year-$month-$day";
echo $ymd;

答案 1 :(得分:0)

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

一步到位:

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

或分为两步:

// 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);