-5

I want to Print last 10 days from the current day. I want to do this by using php. Please can any one help me.

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
Syed Rehaman
  • 39
  • 1
  • 4

2 Answers2

1

The questions is of really low quality, but knowing how few people know this, I'll answer it anyway ...

Use the DateTime classes:

// Current timestamp
$today    = new DateTime();

// For a precise 10 day difference, clone $today
// and substract 10 days from it.
$backdate = clone $today;
$backdate->sub(new DateInterval('P10D'));

// Declare a DatePeriod between the two dates,
// with a 1-day interval in between them
$period = new DatePeriod($backdate, new DateInterval('P1D'), $today);
// Profit
foreach ($period as $date) {
    echo $date->format('Y-m-d'), "\n";
}

It is important to use DatePeriod with a start and end date, as opposed to just telling it do use 10 occurances.
The latter would result in 10 dates in addition to the starting one; in other words - you'd get 11 instead of 10 dates.

Narf
  • 14,600
  • 3
  • 37
  • 66
1
for ($days = 9; $days >= 0; $days--){ print_r( date('d-m-Y',strtotime(date('d-m-Y').' -'.$days.' days'))." "); }

Current date format is in 'd-m-Y'. You may change this to your taste. I'm not sure if you'd like today included. If not so, change 9 into 10 and 0 to 1 in the 'for'-construct and it will give you the 10 days before today (excluding today)...

Werner
  • 449
  • 4
  • 12