generating dates of previous seven days in php

99封情书 提交于 2019-12-24 00:56:31

问题


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:


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.




回答2:


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")));
}



回答3:


Use strtotime()

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

Also see this related question and answers:

  • Add number of days to a date



回答4:


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'));
}
?>



回答5:


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



来源:https://stackoverflow.com/questions/2337968/generating-dates-of-previous-seven-days-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!