Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates calculation - today and N days

Status
Not open for further replies.

gm199

Programmer
Aug 9, 2001
37
US
Few days ago I was programing for a customer that asked to show entered data from a database fom today up to 7 days ago.
Simples task, huh?

I found no resorce with a routine to help me out nor intend to use a hole module just for that, then I come up with this stupid and simple solution that I what to share:

# copyright Gilnei Moraes <gm199@fenomeno.com>

for ($i = 0; $i <= 6; $i++) { # 6 is the desired days

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - (86400*$i)); #86400 is the total seconds in a day
$mday = &quot;0$mday&quot; if ($mday < 10);
$hour = &quot;0$hour&quot; if ($hour < 10);
$min = &quot;0$min&quot; if ($min < 10);
$month = ++$mon;
$month = &quot;0$month&quot; if ($month < 10);

$year = 1900 + $year;
#$count = &quot;$mday-$hour-$min&quot;;
$count = &quot;$mday/$month/$year&quot;;

print &quot;$count<br>\n&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top