-2

based on this code :

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

can someone show me to convert the format from Y-m-d to d-m-Y format ? thanks

hicarl
  • 1
  • 3

1 Answers1

1

try this

$tarikh = mysql_real_escape_string($_POST['tarikh']);
$myDateTime = DateTime::createFromFormat('Y-m-d', $tarikh);
$newDateString = $myDateTime->format('d-m-Y');
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
  • $tarikh = mysql_real_escape_string($_POST['tarikh']); $myDateTime = date('Y-m-d', $tarikh); $newDateString = $myDateTime->date('d-m-Y'); it shown this notice : Notice: A non well formed numeric value encountered in C:\xampp\htdocs\sistem_laporan\tambah_laporan.php on line 208 Fatal error: Call to a member function date() on a non-object in C:\xampp\htdocs\sistem_laporan\tambah_laporan.php on line 209 – hicarl Sep 06 '14 at 05:27
  • 1
    I feel your date in `$_POST['tarikh']` is not valid date, please print `$tarikh` to check exactly what is coming from the `$_POST['tarikh']` – Ram Sharma Sep 06 '14 at 05:40