Time format in PHP

Please tell me, there is a code of the following type:

<small>' ,$topic['description'],'</small>

The description database contains the date and time in the format YYYY-MM-DD HH:MM: SS. How do I change this string so that the output date is in the format DD. MM. YYYY HH:MM? Thanks!

1 answers

Here are three options. The first:

echo date('d.m.Y H:i', strtotime('2017-01-01 15:15:15'));

Second:

$a = new DateTime('2017-01-01 15:15:15');
echo $a->format('d.m.Y H:i');

If you have a variable that contains not only the date and time, but also some text, then something like this:

$b = 'Торопыжка был голодный 2017-01-01 15:15:15 проглотил утюг холодный';

$c = preg_replace(
    '/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/',
    '$3.$2.$1 $4:$5',
    $b
);
echo $c;
 0
Author: Александр Белинский, 2018-05-26 09:02:44