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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date subtraction

Status
Not open for further replies.

Elmserv

Programmer
Sep 25, 2004
72
GB
This is some foxpro code which is not too dissimilar to PHP

dDate = date() // todays date


do while dow(dDate) != 0 // dow = day of week
dDate = dDate - 1 // reduce date by 1
enddo

print dDate // depending on the language this would probably be a sunday.

My problem is depricating the date

I have had a few endless loops but the ISP's server is still running (just)

Richard

 
use date() to determine the day of the week (see for the syntax for returning dow).

to decrement the date use
Code:
$d = date();
do {
 $d = strtodate("minus 1", $d);}
while (whatever your loop is )
 
oops.. strtodate is in my imagination. should be strtotime()
 
Hi Jeff

What I am trying to do is get a start date for a weekly report.

Users will allways put in the wrong date Sun date instead of Sat etc so I was trying to build some code that will step the date backwards until it hits a Sat & that is the start date for the report.

Richard
 
for what's it's worth

Code:
$d = date();
while (date("w",$d) !== 6){  //6=sat, 0=sun
 $d = strtotime("-1 day", $d);
}
 
This is what I have come up with

Code:
$dDate = $_POST['Date'];


do {
 $dDate = strtotime("minus 1", $dDate);}
while (date('w', $dDate) !== '7')
	
		
Print $Ddate;

will test in a few secs
 
you seem to be ignoring my posts.

notwithstanding this: your solution probably won't work. for two reasons.

1. it's unlikely a user will post a date in unix timestamp format.
2. you're decrementing the date even before testing it. this will end up with you being a week off if the user gets the date right.

if you're using user input try the following:

Code:
$d = strtotime(trim($_POST['Date']));
while (date("w",$d) !== 6){  //6=sat, 0=sun
 $d = strtotime("-1 day", $d);
}
 
This is what I finally got to work
Code:
$d = strtotime(trim($_POST['Date']));
while (trim(date("w",$d)) !== '6')
	{  //6=sat, 0=sun
 		$d = strtotime("-1 day", $d);
	}

Print date('Y-m-d',$d);

Many thanks for all the help, I appeared to be ignoring you because of the delay between reading the posts trying something out then reposting. Only to find that further comments had come in.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top