将Date字符串转换为word问题PHP字符串

时间:2014-08-06 20:01:32

标签: php string date

我正在将字符串编号转换为strtotime,然后尝试输出书面日期。这在一定程度上起作用,但问题是日期是错误的。

PHP

$today = date("Y-m-d");

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"));
        $tmpDate = date('Y-m-d', strtotime($start . " + $i days"));
      //  $formattedDates[] = date('Y-m-d', strtotime($tmpDate));
        $formattedDates[] = date('Y-m-d', strtotime($tmpDate));
    }    
    return $formattedDates;
}


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

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

foreach ($formattedDates as $dt)
{
    $date = strtotime($dt);
    echo date('l jS F Y',$date);
}

输出日期

Dates table

应在文字中显示的真实日期

True Dates

我错在哪里输出正确的格式但日期不正确?

2 个答案:

答案 0 :(得分:0)

这是我写的一个日期范围函数,它生成一个格式化日期数组。它使用DateTime()函数,这些函数可以提供比60*60/24更精确的区间。

/**
 * Creates an array of dates between `$start` and `$end`, intervaled by a day
 *
 * @param string $start Start date string
 * @param string $end End date string
 * @param string $format Date format
 * @param boolean $inclusive Whether or not to include the start and end dates
 * @return array Array of dates between the two
 */
    function dateRange($start = null, $end = null, $format = 'Y-m-d', $inclusive = true) {
        if (empty($start) || empty($end)) {
            return array();
        }
        $start = new DateTime($start);
        $end = new DateTime($end);
        if ($inclusive) {
            $end = $end->modify('+1 day');
        } else {
            $start = $start->modify('+1 day');
        }

        $interval = new DateInterval('P1D');
        $period = new DatePeriod($start, $interval, $end);
        $daterange = array();
        foreach ($period as $date) {
            $daterange[] = $date->format($format);
        }
        return $daterange;
    }

和测试用例

function testDateRange() {
        $results = dateRange('2012-02-20', '2012-03-01');
        $expected = array(
            '2012-02-20',
            '2012-02-21',
            '2012-02-22',
            '2012-02-23',
            '2012-02-24',
            '2012-02-25',
            '2012-02-26',
            '2012-02-27',
            '2012-02-28',
            '2012-02-29',
            '2012-03-01'
        );
        $this->assertEquals($expected, $results);

        $results = dateRange('2012-02-24', '2012-03-01', 'M j');
        $expected = array(
            'Feb 24',
            'Feb 25',
            'Feb 26',
            'Feb 27',
            'Feb 28',
            'Feb 29',
            'Mar 1'
        );
        $this->assertEquals($expected, $results);

        $results = dateRange('2012-02-25', '2012-03-01', 'Y-m-d', false);
        $expected = array(
            '2012-02-26',
            '2012-02-27',
            '2012-02-28',
            '2012-02-29'
        );
        $this->assertEquals($expected, $results);
    }

答案 1 :(得分:0)

我运行了你的代码 - 一切都好 http://codepad.org/E18ccpxV