• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

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?
0
PeterErhard
Asked:
PeterErhard
  • 4
  • 4
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
HellmarkCommented:
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
 
HellmarkCommented:
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now