2
$string = this is my message in 12-10-12, Orlando

に変換したい$new = this is my message in 12th October 2012, Orlando

PHPコードを手伝ってください

4

1 に答える 1

4

これで始められるはずです:

function convert_date($hit)
{
    $date = DateTime::createFromFormat("y-m-d", $hit[0]);

    if (!$date) //invalid date supplied, return original string
        return $hit[0]; 

    return $date->format("jS F Y");
}

$string = "this is my message in 12-10-12, Orlando or maybe 13-11-21";
$string = preg_replace_callback("~(\d{2}-\d{2}-\d{2})~", "convert_date", $string);
echo $string; //this is my message in 12th October 2012, Orlando or maybe 21st November 2013

詳細については、phpの日付修飾子DateTime拡張子preg_replace_callbackを参照してください。

于 2012-11-19T01:02:41.983 に答える