Return current date plus 7 days

PhpDate

Php Problem Overview


I'm Trying to get the current date plus 7 days to display.

Example: Today is August 16, 2012, so this php snippet would output August 23, 2012.

   $date = strtotime($date);
   $date = strtotime("+7 day", $date);
   echo date('M d, Y', $date);

Right now, I'm getting: Jan 08, 1970. What am I missing?

Php Solutions


Solution 1 - Php

strtotime will automatically use the current unix timestamp to base your string annotation off of.

Just do:

$date = strtotime("+7 day");
echo date('M d, Y', $date);

Added Info For Future Visitors: If you need to pass a timestamp to the function, the below will work.

This will calculate 7 days from yesterday:

$timestamp = time()-86400;

$date = strtotime("+7 day", $timestamp);
echo date('M d, Y', $date);

Solution 2 - Php

$date = new DateTime(date("Y-m-d"));
$date->modify('+7 day');
$tomorrowDATE = $date->format('Y-m-d');

Solution 3 - Php

If it's 7 days from now that you're looking for, just put:

$date = strtotime("+7 day", time());
echo date('M d, Y', $date);

Solution 4 - Php

$now = date('Y-m-d');
$start_date = strtotime($now);
$end_date = strtotime("+7 day", $start_date);
echo date('Y-m-d', $start_date) . '  + 7 days =  ' . date('Y-m-d', $end_date);

Solution 5 - Php

<?php
print date('M d, Y', strtotime('+7 days') );

Solution 6 - Php

you didn't use time() function that returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). use like this:

$date = strtotime(time());
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);

Solution 7 - Php

This code works for me:

<?php
$date = "21.12.2015";
$newDate = date("d.m.Y",strtotime($date."+2 day"));
echo $newDate; // print 23.12.2015
?>

Solution 8 - Php

echo date('d-m-Y', strtotime('+7 days'));

Solution 9 - Php

$date = strtotime("+7 day", strtotime("M d, Y"));
$date =  date('j M, Y', $date);

This will work as well

Solution 10 - Php

Here is how you can do it using strtotime(),

<?php
    $date = strtotime("3 October 2005");
    $d = strtotime("+7 day", $date);
    echo "Created date is " . date("Y-m-d h:i:sa", $d) . "<br>";
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames ClearView Question on Stackoverflow
Solution 1 - PhpMike MackintoshView Answer on Stackoverflow
Solution 2 - PhpAbduhafizView Answer on Stackoverflow
Solution 3 - PhpwessideView Answer on Stackoverflow
Solution 4 - PhpMahmoud ZaltView Answer on Stackoverflow
Solution 5 - PhpMetalFrogView Answer on Stackoverflow
Solution 6 - PhpMKTView Answer on Stackoverflow
Solution 7 - PhpLeonZoView Answer on Stackoverflow
Solution 8 - PhpMuhammad AzeemView Answer on Stackoverflow
Solution 9 - PhpDozeeyView Answer on Stackoverflow
Solution 10 - Phpam96enView Answer on Stackoverflow