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

Increment Date Variable 1

Status
Not open for further replies.

stoolpigeon

Programmer
Aug 1, 2001
309
US
I am curious if anyone can tell me why the first example works and the second does not.
Code:
$sp1 = date("Y-m-d",strtotime($data["start_dt"])-1);
If the value of $data["start_dt"] is 2002-11-10 then the above returns 2002-11-09, which is what I want.

But this statement
Code:
$sp1 = date("Y-m-d",strtotime($data["start_dt"])+7);
For the same value 2002-11-10 is returned. The seven days are not added. I can remove the +7 part and loop through adding one at a time and that works. (that's what I'm doing for now) But I want to understand why I can't add more than one.

What I've done for now looks like this:
Code:
$sp1 = date("Y-m-d",strtotime($data["start_dt"]));
for($i=0;$i<7;$i++) $sp1++;
That gets me where I want to go but seems less than ideal.
 
I strongly recommend that you read the PHP online manual section for strtotime(). It is available at
You're not subtracting or adding days, you're subtracting or adding seconds. It just so happens that if you give strtotime() a date/time string that is just a date, it returns the Current Unix Epoch time tick that represents the first second of that day. Subtract one day from it, and you have the last second of the previous day. Add seven seconds to that number, and you are seven seconds into the current day.

Convert all date math to numbers of seconds. ______________________________________________________________________
TANSTAAFL!
 
I read it - I promise, I just didn't put it all together. Here is a great example of habits blinding the mind. (I do lots of VB work)

Thanks for the help- everything works very well now.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top