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 回答