简体   繁体   中英

Determining percentages and past seven days

I am coming here with a fairly simple (i think) question today. I would like to figure out the past seven days with php

So: I have todays date in variables (like $day = 2, $month = 5, $year = 2013 , all put together 2/5/2013), My question is How would i go about getting the past seven days (in the same format) such as in this case

2/5/2013
1/5/2013
30/4/2013
29/4/2013
28/4/2013
27/4/2013
26/4/2013

I have tried subtracting the days for each variable (like $day6 = $todays_date - 1; ) but getting month and year changes from that would be quite difficult i believe.

any answers would be appreciated.

Use DateTime class and its modify method

$date = new DateTime();
$yesterday = $date->modify('-1 day');

You could use mktime, and subtract the number of seconds for each day:

$today = mktime(0,0,0,$month,$day,$year);
for($i=0;$i<=6;$i++){
    echo date('j/n/Y',$today-($i*(24*60*60))) . '<br />;
}

Try this:

$day = '02/05/2013';

$dates = array();
for($i = 0; $i < 7; $i++){
    $dates[] = date('Y-m-d', strtotime($day . ' -' . $i . 'days'));
}

print_r('<pre>');
print_r($dates);
die();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM