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?
<?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?
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
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;
This is a PHP object.
You shall use
$fileDate->format('Y/m/d') ;
You shall use
$fileDate->format('Y/m/d')
Well..
echo $fileDate->format('Y/m/d') ;
echo $fileDate->format('Y/m/d')
ASKER
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.p hp on line 25
$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
Try to remove the "s" from days"
$fileDate = new Date("Y-m-d");
$fileDate->modify("+7 day");
echo $fileDate->format('Y-m-d');
strtotime by default uses UNIX time format, so need to format it.
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?
We can stream line that even more, by just doing away with the variables entirely.
<?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>";
?>
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>";
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
?>
ASKER
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.
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"));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks HellMark, that does the trick perfectly and exactly what I was after.
Thanks so much. All looks a lot more simple now.
Thanks so much. All looks a lot more simple now.
Open in new window