-2

In the database, date format is Y-m-d, I'm trying to display my result with this format d-m-Y.

This is my code:

<input name="personal_ic_from" type="text" class="tcal" id="personal_ic_from" value="<?php echo $personal_ic_from ?>" readonly="readonly"/>
B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
afifi
  • 85
  • 1
  • 5
  • 12

1 Answers1

1

Follow the procedural style in php

<?php
$databaseDate = '2013-09-29 01:02:03';
$date = date_create($databaseDate);
echo date_format($date, 'd-m-y');
?>

You can also try strtotime()

<?php
$queryResultDate = mysqli_query("SELECT column_name FROM table_name"); //selecting the  date column
 $date = date("d-m-y" , strtotime($queryResultDate));
echo $date;
?>
raduns
  • 765
  • 7
  • 18
  • this code will echo the date for today.. i want it echo the date on my database that i saved. – afifi Sep 30 '13 at 06:30
  • @afifi: this is the example. Replace `$databaseDate` with your variable `$personal_ic_from`. – Glavić Sep 30 '13 at 08:21
  • @Glavić, i try it, the result is 2013-09-29 01:02:03, but my $personal_ic_from in my database is 2013-09-01. i required to echo that value(date value) from the database in this format d-m-Y and $personal_ic_from type is date not datetime – afifi Sep 30 '13 at 08:40
  • @afifi: yes, because you copy/pasted the above code, and didn't change anything, ccc. Try: `echo date_create($personal_ic_from)->format('d-m-Y');` – Glavić Sep 30 '13 at 08:42
  • @Glavić, ok thx bro.. i understand now.. if i want to update the date, i still need to convert back ? – afifi Sep 30 '13 at 08:47
  • @Glavić, this my 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"])."'"); i still need to convert back from d-m-Y to Y-m-d? – afifi Sep 30 '13 at 08:51
  • @afifi ofc. If you have [standard date format](http://www.php.net/manual/en/datetime.formats.date.php) (which currentlly is) you can convert it back in the same maner. For unknown formats look at the answer [here](http://stackoverflow.com/a/2167925/67332). – Glavić Sep 30 '13 at 08:56
  • @Glavić, i cant understand link for the answer.. can u show me, where should i put the format converter code inside my update query?? it will be big help for me because i need to finish my assignment and i stuck here.. – afifi Sep 30 '13 at 09:15
  • @afifi - You need to put the format converter code for the variable which you want to send to the update query.The above code is just a sample for converting date formats in php and you can do by that way. – raduns Sep 30 '13 at 09:26