2

my date type is date and the data is 2010-08-01 I convert from Y-m-d to d-m-Y when echo output - 01-08-2010

echo code

<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>

what should I add on my update query to convert from d-m-Y to Y-m-d. I found that my database cant store the new date data because the database format is date Y-m-d

update code

<?php 
$personal_ic_from = $_POST['personal_ic_from'];
mysql_query("UPDATE personal_id_ic SET personal_ic_from = '".mysql_real_escape_string($_POST["personal_ic_from"])."'  WHERE LAS_login_id = '".mysql_real_escape_string($_POST["LAS_login_id"])."'");
?>
4

5 回答 5

1

Please refer to STR_TO_DATE(). Your query should look like this,

mysql_query("UPDATE personal_id_ic SET personal_ic_from = STR_TO_DATE('".mysql_real_escape_string($_POST["personal_ic_from"])."','%Y-%m-%d')  WHERE LAS_login_id = '".mysql_real_escape_string($_POST["LAS_login_id"])."'");
于 2013-10-01T04:29:24.453 回答
0

just use STR_TO_DATE function in your query it's mysql function. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

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

于 2013-10-01T04:33:50.643 回答
0
<?php 
 $personal_ic_from = date("Y-m-d", strtotime($_POST['personal_ic_from']));
 mysql_query("UPDATE personal_id_ic SET personal_ic_from = '".mysql_real_escape_string($personal_ic_from)."'  WHERE LAS_login_id = '".mysql_real_escape_string($_POST["LAS_login_id"])."'");
?>

Try this. This will solves your issue.

于 2013-10-01T04:40:00.850 回答
0

I've recently learned about "explode" on this forum and have implemented it like this:

$frontenddob = $POST['dob'];    
list ($m, $d, $y) = explode('/', $frontenddob);    
$dob = sprintf("%04d-%02d-%02d", $y, $m, $d);

You can use it to convert between date formats, and don't run into the problems with strtotime date limits.

于 2013-10-01T04:42:05.710 回答
0
Try this code
<?php 
$personal_ic_from = $_POST['personal_ic_from'];
$personalDate=date_create($personal_ic_from);
$pdate=date_format($personalDate,"d-m-Y");

mysql_query("UPDATE personal_id_ic SET personal_ic_from ='".mysql_real_escape_string($pdate)."'  WHERE LAS_login_id = '".mysql_real_escape_string($_POST["LAS_login_id"])."'");

?>
于 2013-10-01T04:43:53.147 回答