如何在php中以相反的顺序获取日期?

如何在php中以相反的顺序获取日期?,php,arrays,date,Php,Arrays,Date,我构建了一个php页面,在其中我希望以相反的顺序获取日期。我是如何做到这一点的 这是我的密码: $dates = array(); $timestamp = time(); for ($i = 0 ; $i <=30 ; $i++) { $dates[$i]= date('m-d-Y', $timestamp); $timestamp -= 24 * 3600; } 如何以相反的顺序存储日期?相反您可以使用strotime函数 <?php $dates = arra

我构建了一个php页面,在其中我希望以相反的顺序获取日期。我是如何做到这一点的

这是我的密码:

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

如何以相反的顺序存储日期?

相反您可以使用strotime函数

<?php 

$dates = array();
//get the last day and go from that day
$timestamp = strtotime('-30 days');

for ($i = 0 ; $i <=30 ; $i++) {

   //insert the date
   $dates[$i]= date('m-d-Y', $timestamp);

   //increase the day
    $timestamp += 24 * 3600;
}

//display the output
print_r($dates);


Array
(
    [0] => 01-02-2014
    [1] => 01-03-2014
    [2] => 01-04-2014
    [3] => 01-05-2014
    [4] => 01-06-2014
    [5] => 01-07-2014
    [6] => 01-08-2014
    [7] => 01-09-2014
    [8] => 01-10-2014
    [9] => 01-11-2014
    [10] => 01-12-2014
    [11] => 01-13-2014
    [12] => 01-14-2014
    [13] => 01-15-2014
    [14] => 01-16-2014
    [15] => 01-17-2014
    [16] => 01-18-2014
    [17] => 01-19-2014
    [18] => 01-20-2014
    [19] => 01-21-2014
    [20] => 01-22-2014
    [21] => 01-23-2014
    [22] => 01-24-2014
    [23] => 01-25-2014
    [24] => 01-26-2014
    [25] => 01-27-2014
    [26] => 01-28-2014
    [27] => 01-29-2014
    [28] => 01-30-2014
    [29] => 01-31-2014
    [30] => 02-01-2014
)

您可以反转数组:我想要数组[0]=>01-02-2014,等等$dates[$i]=date'd-m-Y',$timestamp;你不明白我的问题,我想从上面的数组中反转数组我想要数组[0]=数组[30]
<?php 

$dates = array();
//get the last day and go from that day
$timestamp = strtotime('-30 days');

for ($i = 0 ; $i <=30 ; $i++) {

   //insert the date
   $dates[$i]= date('m-d-Y', $timestamp);

   //increase the day
    $timestamp += 24 * 3600;
}

//display the output
print_r($dates);


Array
(
    [0] => 01-02-2014
    [1] => 01-03-2014
    [2] => 01-04-2014
    [3] => 01-05-2014
    [4] => 01-06-2014
    [5] => 01-07-2014
    [6] => 01-08-2014
    [7] => 01-09-2014
    [8] => 01-10-2014
    [9] => 01-11-2014
    [10] => 01-12-2014
    [11] => 01-13-2014
    [12] => 01-14-2014
    [13] => 01-15-2014
    [14] => 01-16-2014
    [15] => 01-17-2014
    [16] => 01-18-2014
    [17] => 01-19-2014
    [18] => 01-20-2014
    [19] => 01-21-2014
    [20] => 01-22-2014
    [21] => 01-23-2014
    [22] => 01-24-2014
    [23] => 01-25-2014
    [24] => 01-26-2014
    [25] => 01-27-2014
    [26] => 01-28-2014
    [27] => 01-29-2014
    [28] => 01-30-2014
    [29] => 01-31-2014
    [30] => 02-01-2014
)