$string = this is my message in 12-10-12, Orlando
我想将其转换为$new = this is my message in 12th October 2012, Orlando
.
请帮我编写 php 代码
最佳答案
这应该可以帮助您开始:
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 date modifiers ,DateTime extension和 preg_replace_callback欲了解更多信息。
关于php - 在php中转换文本字符串中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446316/