将日期数字字符串转换为Word of day PHP

时间:2014-08-06 19:28:14

标签: php date

我有一个PHP脚本输出日期,如2014-08-06,我希望将它们输出为Wednesday 6th August 2014

我之前已经在使用这个数字所以我需要从变量中取出这个数字字符串并转换它。

PHP

function dateRange($start, $end) {
    date_default_timezone_set('UTC');

    $diff = strtotime($end) - strtotime($start);

    $daysBetween = floor($diff/(60*60*24));

    $formattedDates = array();
    for ($i = 0; $i <= $daysBetween; $i++) {
        $tmpDate = date('Y-m-d', strtotime($start . " + $i days"));
        $formattedDates[] = date('Y-m-d', strtotime($tmpDate));
    }    
    return $formattedDates;
}


$start=$date_system_installed;
$end=$today;

$formattedDates = dateRange($start, $end);

    foreach ($formattedDates as $dt)
{
 echo $dt; //this is where i wish to change the number to the word/s. 
}

2 个答案:

答案 0 :(得分:0)

您可以使用DateTime::createFromFormatDateTime::format创建DateTime对象,然后以您想要的格式创建字符串:

echo DateTime::createFromFormat('Y-m-d', $dt)->format('l jS F Y');

理想情况下,我要做的是一直使用DateTime对象:

function dateRange($start, $end) {
    date_default_timezone_set('UTC');
    $daysBetween = $start->diff($end)->format('%R%a');
    $formattedDates = array();
    for ($i = 0; $i <= $daysBetween; $i++) {
        $formattedDates[] = clone $start->modify('+1 day');
    }    
    return $formattedDates;
}

$start = DateTime::createFromFormat('Y-m-d', "2014-08-01");
$end = new DateTime;
$formattedDates = dateRange($start, $end);
foreach ($formattedDates as $dt)
{
    echo $dt->format('l jS F Y');
}

在此处查看:http://sandbox.onlinephpfunctions.com/code/2003646deb0b39d501d7e49eb23edc3979a10762

答案 1 :(得分:0)

您可以使用功能date http://php.net//manual/en/function.date.php

例如:

<?php

    $date = strtotime('2014-08-06');
    echo date('l jS F Y', $date);  //Wednesday 6th August 2014
    /*
        l - full name of week day, jS - day of month with suffix,
        F - full name of month, Y - year
    */
相关问题