0

As you know, date format from a database is Y-m-d, I need to echo the date in d-m-Y. This is the code for 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>

My problem is, after the user changes the date of personal_ic_from, I need to convert back the date format from d-m-Y to Y-m-d (which is the standard format for date in database) in update query. However, I can't manage to do that..

This my update query:

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

?>

How can I convert back the format from d-m-Y to Y-m-d so database can save the new date? I tried to change the query to for personal_id_ic , but when I use this code the database saved a random date, not the date that user choose:

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"])."'");
mr2ert
  • 5,146
  • 1
  • 21
  • 32
afifi
  • 85
  • 1
  • 5
  • 12

2 Answers2

2

It easily can achieve with this simple short code ...

$dmy = "20-12-2013";
list($day, $month, $year) = explode("-", $dmy);
$ymd = "$year-$month-$day";
echo $ymd;
yan
  • 21
  • 2
  • Yes this is ... But for your requirement it just an easy task to save the date into variable and then use it in your query ... – yan Oct 01 '13 at 06:19
  • ok yan, I solved my problem. thx for some idea. – afifi Oct 01 '13 at 06:25
0

You can use PHP's strtotime function to convert the date into a Unix timestamp. From there, you can use PHP's date function to format it in the way you want.

In one step:

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

Or broken down into 2 steps:

// 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);
  • i try replace '$personal_ic_from = $_POST['personal_ic_from'];' to '$personal_ic_from = date('Y-m-d', strtotime($_POST['personal_ic_from']));' , and use the update query '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"])."'"); the result still the same. Any suggestion? – afifi Oct 01 '13 at 03:47
  • Not sure about your query, but you should check if the date has been formatted correctly by doing an echo on $personal_ic_from – JPWeston-Smith Oct 01 '13 at 20:03
  • ok thx.. i solve using this query 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"])."'"); thx for the idea – afifi Oct 02 '13 at 00:40