如何在PHP中转换日期格式?

时间:2014-02-01 13:47:49

标签: php date

我在这里制作一个php页面,我的日期分配给融合图表的标签 如何将日期格式从01-02-2014转换为1月2日? 这是我的代码:

$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
   $dates[$i]= date('m-d-Y', $timestamp);
    $timestamp -= 24 * 3600;
}

3 个答案:

答案 0 :(得分:1)

这将有助于您进一步

<?php
$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
   $dates[$i]= date('M j', $timestamp);
    $timestamp -= 24 * 3600;
}

print_r($dates);

Array
(
    [0] => Feb 1
    [1] => Jan 31
    [2] => Jan 30
    [3] => Jan 29
    [4] => Jan 28
    [5] => Jan 27
    [6] => Jan 26
    [7] => Jan 25
    [8] => Jan 24
    [9] => Jan 23
    [10] => Jan 22
    [11] => Jan 21
    [12] => Jan 20
    [13] => Jan 19
    [14] => Jan 18
    [15] => Jan 17
    [16] => Jan 16
    [17] => Jan 15
    [18] => Jan 14
    [19] => Jan 13
    [20] => Jan 12
    [21] => Jan 11
    [22] => Jan 10
    [23] => Jan 9
    [24] => Jan 8
    [25] => Jan 7
    [26] => Jan 6
    [27] => Jan 5
    [28] => Jan 4
    [29] => Jan 3
    [30] => Jan 2
)

答案 1 :(得分:0)

您可以在php.net/manual/en/function.date.php

查看所有格式选项
 $dates[$i]= date('d M', $timestamp);

输出:

Array
(
    [0] => 01 Feb
    [1] => 31 Jan
    [2] => 30 Jan
    [3] => 29 Jan
    [4] => 28 Jan
    .............
)

答案 2 :(得分:0)

使用DateTime::setTimestamp

<?php

$timestamp = time();
for ($i = 0 ; $i <=30 ; $i++) {
$date = new DateTime();
$date->setTimestamp($timestamp);
 $dates[$i]=$date->format('M d') . "\n";
 $timestamp -= 24 * 3600;
}

print_r($dates);

输出:

 Array
    (
        [0] => Feb 01

        [1] => Jan 31

        [2] => Jan 30

     ....
)

Demo