Link to home
Start Free TrialLog in
Avatar of PeterErhard
PeterErhard

asked on

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?
Avatar of djon2003
djon2003
Flag of Canada image

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

Avatar of PeterErhard
PeterErhard

ASKER

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

This is a PHP object.

You shall use

$fileDate->format('Y/m/d');
Well..

echo $fileDate->format('Y/m/d');
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
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

Avatar of Keith Brown
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

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

ASKER CERTIFIED SOLUTION
Avatar of Keith Brown
Keith Brown
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks HellMark, that does the trick perfectly and exactly what I was after.

Thanks so much. All looks a lot more simple now.