简体   繁体   English

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

[英]Converting a Date Number String to Word of day PHP

I have a PHP script outputting dates like this 2014-08-06 and i wish to make them output as Wednesday 6th August 2014 instead. 我有一个PHP脚本输出类似2014-08-06这样的日期,我希望将它们输出为Wednesday 6th August 2014Wednesday 6th August 2014

I am already using the number previously so i need to take this from the variable of which holds the number string and convert that. 我以前已经在使用数字,所以我需要从保存数字字符串的变量中获取该数字并将其转换。

The PHP 的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. 
}

You can use DateTime::createFromFormat and DateTime::format to create a DateTime object and then create a string in the format you want: 您可以使用DateTime::createFromFormatDateTime::format创建DateTime对象,然后以所需的格式创建字符串:

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

Ideally what I would do is work with DateTime object all the way through: 理想情况下,我会一直使用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');
}

See it working here: http://sandbox.onlinephpfunctions.com/code/2003646deb0b39d501d7e49eb23edc3979a10762 看到它在这里工作: http : //sandbox.onlinephpfunctions.com/code/2003646deb0b39d501d7e49eb23edc3979a10762

You can use function date http://php.net//manual/en/function.date.php 您可以使用函数date http://php.net//manual/en/function.date.php

For example: 例如:

<?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
    */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM