Home » Php » php – Get the date of one week from today

php – Get the date of one week from today

Posted by: admin April 23, 2020 Leave a comment

Questions:

How do I get the date, one week from today, in the following format: YYYY-MM-DD ?

How to&Answers:

Try:

date("Y-m-d", strtotime("+1 week"));

This will output:

2015-12-31

If today is 2015-12-24

Answer:

Just so Charles’ prediction is wrong, here’s a PHP 5.3+ example:

$now = new DateTime;
$interval = new DateInterval('P1W')
$next_week = $now->add($interval);
echo $next_week->format('Y-m-d');

or in slightly more compact form:

$now = new DateTime();
echo $now->add(new DateInterval('P1W'))->format('Y-m-d');

Answer:

<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";

One missing from Charles prediction, straight from the horses mouth, example #1

Answer:

adding days, weeks, months to any date

 $date = date("Y-m-d");// current date

 $date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");

 $date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");

 $date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");

 $date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");

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