user5422072
user5422072

Reputation:

How to display date in descending order

I want to Print the date of last 30 days in descending order.

Ex: 

    2015-10-16
    2015-10-15
    2015-10-14
    2015-10-13
    2015-10-12
    2015-10-11
    2015-10-10
    2015-10-09
    2015-10-08
    2015-10-07
    2015-10-06
    2015-10-05
    2015-10-04
    2015-10-03
    2015-10-02
    2015-10-01
    2015-09-30
    2015-09-29

and so on.....

Please note that it should be dynamic. it should change daily.. what i mean is .. the top date should be today's date..

This is what i have tried..

<?php

echo "<select class='input-dropdown2' name='coupon_validity'>";

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

$timestring=date('Y-m-d');
$datetime=new DateTime($timestring);
$datetime->modify('-$i day');
$y=$datetime->format('Y-m-d'); 

echo "<option value='" . $y ."'".">" . $y ."</option>"; 


}


echo "</select>";

?>

Upvotes: 0

Views: 2185

Answers (1)

Hussy Borad
Hussy Borad

Reputation: 583

Try this code :-

<?php

        for ($i = 0; $i < 30; $i++)
        {
            $timestamp = time();
            $tm = 86400 * $i; // 60 * 60 * 24 = 86400 = 1 day in seconds
            $tm = $timestamp - $tm;

            echo  $the_date = date("Y-m-d", $tm);
            echo "<br/>";
        }

?>

Upvotes: 1

Related Questions