views:

48

answers:

5

I need help regarding generating dates and days of the last seven to ten days with respect to today. How an accurate timestamp can be created which can take care of week, month and year change ?

+1  A: 

Use strtotime()

for($i = 1; $i <= 10; $i++){
    echo strtotime("-$i days"), PHP_EOL;
}

Also see this related question and answers:

Gordon
I like this... very lateral...
Allain Lalonde
+1  A: 

Easiest way would be to get a timestamp that corresponds to today, using the time() function ; and 7 times remove 1 day to that timestamp, each echoing the date that corresponds to that timestamp :

$timestamp = time();
for ($i = 0 ; $i < 7 ; $i++) {
    echo date('Y-m-d', $timestamp) . '<br />';
    $timestamp -= 24 * 3600;
}

Which will get you this output :

2010-02-25
2010-02-24
2010-02-23
2010-02-22
2010-02-21
2010-02-20
2010-02-19

Because a timestamp only represents the number of seconds since 1970-01-01, substracting one day to a timestamp means substracting 24*3600 seconds.


Edit after the comments :

For month and weeks changes, this will still work : one day is still 24*3600 seconds.

For instance, if you test using this line instead of the first one I posted in my first example :

$timestamp = strtotime('2010-02-03');

You'll get this output :

2010-02-03
2010-02-02
2010-02-01
2010-01-31
2010-01-30
2010-01-29
2010-01-28

It changed of month fine.


For leap years, if I test using this :

$timestamp = strtotime('2008-03-03');

I get :

2008-03-03
2008-03-02
2008-03-01
2008-02-29
2008-02-28
2008-02-27
2008-02-26

Which looks fine, as there were 29 days in february 2008.

And if I test with :

$timestamp = strtotime('2009-03-03');

I get :

2009-03-03
2009-03-02
2009-03-01
2009-02-28
2009-02-27
2009-02-26
2009-02-25

Which looks fine too.

Pascal MARTIN
this won't handle leap years
Allain Lalonde
and what about the month and week change...?
Anurag
@Allain and @Anurag : I've edited my post to give a couple of other examples ;; do you have a specific case in which this doesn't work ?
Pascal MARTIN
If you're only displaying full dates, you won't notice a difference, but if you display timestamps (date+time you'll see the problem.
Allain Lalonde
won't handle dst either. It may skip a day because of that.
Artefacto
A: 

To generate the timestamp of 12am for the last 10 days (including today) the following code will do:

<?php
for ($i=0; $i<=10; $i++) {
  echo mktime (0, 0, 0, date('m'), date('d')-$i, date('Y'));
}
?>
Allain Lalonde
Will it handle month and week change. And what about a leap year..?
Anurag
A: 

Everything you need you can find in this page of the php manual.

jeroen
+1  A: 

If you're lucky enough to be able to play with PHP 5.3 (why not?) then the DateTime/Interval/Period classes will also come in exceptionally useful. The following example prints the dates for the previous 7 days (along with today) relative to today.

$start  = new DateTime('-7 days', new DateTimeZone('UTC'));
$period = new DatePeriod($start, new DateInterval('P1D'), 7);

foreach ($period as $date) {
    var_dump($date->format('D d-m-Y'));
}

Which outputs something like:

string(14) "Thu 18-02-2010"
string(14) "Fri 19-02-2010"
string(14) "Sat 20-02-2010"
string(14) "Sun 21-02-2010"
string(14) "Mon 22-02-2010"
string(14) "Tue 23-02-2010"
string(14) "Wed 24-02-2010"
string(14) "Thu 25-02-2010"

Similar can be done relatively easily with strtotime as in the snippet below (which produces the same output as above):

for ($i = 7; $i >= 0; $i--) {
    var_dump(date('D d-m-Y', strtotime("-$i days")));
}
salathe