Adding days to php date

I've got this example from a website:

<?php
$currentDate = date("Y-m-d");// current date

echo "Current Date: ".$currentDate."<br>";

//Add one Date
$date = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 day");
echo $date . "<br>";

$date = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 week");
echo $date . "<br>";
$date = strtotime(date("Y-m-d", strtotime($currentDate)) . " +2 week");
echo $date . "<br>";
$date = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 month");
echo $date . "<br>";

$date = strtotime(date("Y-m-d", strtotime($currentDate)) . " +30 days");

echo $date . "<br>";
?>

which displays the output as:

Current Date: 2011-10-29
1319950800
1320469200
1321077600
1322546400
1322460000

How can I get it to display the other dates as actual dates rather than what appear to be time stamps?
PeterErhardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

djon2003Commented:
Here is the code I use to add one hour.
$fileDate = new DateTime(date("Y/m/d H:i"));
$fileDate->modify("+1 hour");

Open in new window

0
PeterErhardAuthor Commented:
Thanks, but no good in just doing a copy/paste of what you posted:

Catchable fatal error: Object of class DateTime could not be converted to string in /home/..... on line 26


$fileDate = new DateTime(date("Y/m/d H:i"));
$fileDate->modify("+1 hour");

echo $fileDate;

Open in new window

0
djon2003Commented:
This is a PHP object.

You shall use

$fileDate->format('Y/m/d');
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

djon2003Commented:
Well..

echo $fileDate->format('Y/m/d');
0
PeterErhardAuthor Commented:
Thanks, still having trouble though as I want to add days and not hours:

$fileDate = new Date("Y-m-d");
$fileDate->modify("+7 days");

echo $fileDate->format('Y-m-d');

Fatal error: Class 'Date' not found in /home/cricketw/public_html/football/datetest.php on line 25
0
djon2003Commented:
Try to remove the "s" from days"
$fileDate = new Date("Y-m-d");
$fileDate->modify("+7 day");

echo $fileDate->format('Y-m-d');

Open in new window

0
HellmarkLinux Systems AdministratorCommented:
strtotime by default uses UNIX time format, so need to format it.

<?php
$currentDate = date("Y-m-d");// current date
echo "Current Date: ".$currentDate."<br>";

//Add one Date
$date = date("Y-m-d, strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 day"));
echo $date . "<br>";

$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 week"));
echo $date . "<br>";  
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($currentDate)) . " +2 week"));
echo $date . "<br>";  
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 month"));
echo $date . "<br>";  
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($currentDate)) . " +30 days"));
echo $date . "<br>";
?>

Open in new window


That's what you would need to do with existing code, however, it is a bit overblown. That code isn't very efficient. No need to use the $currentDate variable, because PHP defaults to using that anyway if you don't specify. So why add the time, convert that to a date,  spit that string to be converted to a time, only to be processed back as a date?

<?php
$currentDate = date("Y-m-d");// current date
echo "Current Date: ".$currentDate."<br>";
//Add one Date
$date = date("Y-m-d", strtotime("+1 day"));
echo $date . "<br>";
$date = date("Y-m-d", strtotime("+1 week"));
echo $date . "<br>";
$date = date("Y-m-d", strtotime("+2 week"));
echo $date . "<br>";  
$date = date("Y-m-d", strtotime("+1 month"));
echo $date . "<br>";  
$date = date("Y-m-d", strtotime("+30 days"));
echo $date . "<br>";

Open in new window


We can stream line that even more, by just doing away with the variables entirely.
<?php
echo "Current Date: " . date("Y-m-d") . "<br />"; //Current Date
echo date("Y-m-d", strtotime("+1 day")) . "<br />"; //Tomorrow
echo date("Y-m-d", strtotime("+1 week")) . "<br />"; //7 Days from now
echo date("Y-m-d", strtotime("+2 week")) . "<br />"; //14 Days from now
echo date("Y-m-d", strtotime("+1 month")) . "<br />"; //30 Days from now
echo date("Y-m-d", strtotime("+30 days")) . "<br />"; //Also 30 days from now
?>

Open in new window

0
PeterErhardAuthor Commented:
Thanks Hellmark, that gives me some good basis to work from.

I've got what I need working now with the below, but it does seem overly complex and messy.
$CompetitionStartDate = $_POST["YearStart"] . "-" . $_POST["MonthStart"] . "-" . $_POST["DayStart"];

$End = date('Y-m-d',strtotime(date("Y-m-d", strtotime($CompetitionStartDate)) . " +7 day"));

Open in new window

0
HellmarkLinux Systems AdministratorCommented:
You have a bit of redundant code there. You convert the string of the competition to a time, convert to a date, add the +7 day to that (which makes it a string again), convert that string to a timestamp, and convert that to a date again. Why not just take the string of the startdate, add the "+7 day", make it into a time, and process that as a date?

$CompetitionStartDate = $_POST["YearStart"] . "-" . $_POST["MonthStart"] . "-" . $_POST["DayStart"];
$End = date('Y-m-d',strtotime($CompetitionStartDate . " +7 day"));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterErhardAuthor Commented:
Thanks HellMark, that does the trick perfectly and exactly what I was after.

Thanks so much. All looks a lot more simple now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.