php - 在php中转换文本字符串中的日期格式

标签 php string date

$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 modifiersDateTime extensionpreg_replace_callback欲了解更多信息。

关于php - 在php中转换文本字符串中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446316/

相关文章:

android - date.getTIme() 的负值

java - 日期和 GregorianCalendar 毫秒处理

PHP 显示 97% 的 CPU 使用率

PHP运行时类修改

javascript - 在Javascript中,如何将字符串转换为属性名称?

mysql - Powershell MySQL 字符串返回 |到 html?

php - 调用非对象的成员函数 fill()

php - PDO::FETCH_OBJECT 错误

c++ - 添加该行的所有总和

java - 如何在格式化的日期字符串中包含毫秒?