-3

I want to change the date format in a text area,

<textarea tabindex="0"
class="input_lkl  "
name=""><?php echo $class['new']['ord_added_usr']; ?></textarea>

and it show me the date format like this

[120] Added by [admin] on [2016-05-18]

and I want to change the format into Y-m-d to d-m-Y

[120] Added by [admin] on [18-05-2016]
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
jdoe
  • 17
  • 6

3 Answers3

0

extract the date from string and replace it with new format forming by

date function,

    $ori_str = $class['new']['ord_added_usr'];

    $extracted_date = substr($ori_str, strripos($ori_str,'[')+1 , 10);

    $new_date_format = date('d-m-Y',strtotime($extracted_date));


    $new_str = substr($ori_str, 0, strripos($ori_str,'[')+1).$new_date_format.']';
Hytool
  • 1,358
  • 1
  • 7
  • 22
  • How to extract it? var ctime = createdtime.match(/^\s*([0-9]+)\s*-\s*([0-9]+)\s*-\s*([0-9]+)(.*)$/); how to put regex in php? – jdoe May 18 '16 at 09:16
0

First extract the date with regex , something like this: regex to get date

Then format your date with date function, for reference: date()

Or use this function:

function formatDate($date, $divider = "-") {
    //Format date from MYSQL YYYY-MM-DD HH:MM to DD-MM-YYYY
    $hour= substr($date, strpos($date, " "));
    $year= substr($date, 0, 4);
    $month= substr($date, 5, 2);
    $day= substr($date, 8, 2);

    return $day. $divider . $month. $divider . $year;
}
Community
  • 1
  • 1
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
0

Assuming $class['new']['ord_added_usr'] holds [120] Added by [admin] on [2016-05-18]

You can use preg_replace_callback

echo preg_replace_callback("/(\d{4}-\d{2}-\d{2})/", function($matches){
    return date("d-m-Y",strtotime($matches[0]));
},$class['new']['ord_added_usr']);

Result:

[120] Added by [admin] on [18-05-2016]

Demo

Thamilhan
  • 13,040
  • 5
  • 37
  • 59