Rick Joe Rick Joe - 4 months ago 20
PHP Question

Get date in a string php

I want to get a date in a string in php and convert this date to other format:


eg: today is 02/03/2016


$string = preg_replace('~(\d{2})/(\d{2})/(\d{2,4})~', '', $string);


but it is only erasing the date. it will print:


today is


after get the date I want to change to other format:

$date = new DateTime($string);
$result = $date->format('F d, Y');


but, how to get only the date in the string? without erase it?

Answer
$string = 'today is 02/03/2016';
$string = preg_match('/\d{2}\/\d{2}\/\d{4}/', $string, $date);
$date = new DateTime($date[0]);
$result = $date->format('F d, Y');
Comments