2

I'm trying to get my function to add 1 day to the date I send it ,but I can;t figure out how to match the MySQL formatting. I've currently got:

$result = mysql_query($query);
$lastdate = mysql_result($result, 0, 'date');
$date = strtotime(date("Y-m-d", strtotime($lastdate)) . " +1 day");

I know the $date = line is incorrect somewhere, but I don't understand the function too well. It's being given the date in the format YYYY-mm-dd ($query is just getting the last date in the database), due to how MySQL stores dates.
I'm guessing that using the strtotime function isn't the right thing to do, or I've got the format/idea all wrong.
Thanks for any help, this is annoying me now :(

1
  • What type of data is in your 'date' column? Integer? Date? Datetime? Commented Oct 11, 2011 at 20:50

2 Answers 2

5

I think you want $date = date("Y-m-d", strtotime("+1 day", $lastdate)).

You may need to convert $lastdate using strtotime.

Sign up to request clarification or add additional context in comments.

Comments

0

You could just add 86400 seconds to the result from strtotime(), since that returns an integer number of seconds since 1/1/1970, and there are 86400 seconds in a day.

$lastdate = '2011-10-11 22:07:11';
$date = date("Y-m-d", strtotime($lastdate) + 86400);
echo $date;

Outputs:

2011-10-12

2 Comments

You could do that. . . but it isn't as clear what you are doing.
Personally, I think it's clear; but I recognise it as the number of seconds in a day. It could be written as (60*60*24), or commented to make it clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.