Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date format YYYY-MM-DD minus or add one week from now?

Tags:

php

today 22-05-2011 so it should be 29-05-2011? ( plus 1 week ) 
or
today 22-05-2011 so it should be 15-05-2011? ( minus 1 week ) 

thanks for looking in.

Adam Ramadhan

like image 303
Adam Ramadhan Avatar asked May 22 '11 06:05

Adam Ramadhan


People also ask

How do I add 7 days to a date?

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

How can I get current date from next week in PHP?

just used it - date('Y-m-d H:i:s', strtotime('+1 week')); works fine in PHP 7 to give one week from now.

How convert date from yyyy mm dd to dd-mm-yyyy format in PHP?

Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.

How can I add the one day with date in PHP?

Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds. Syntax: date_add(object, interval);


3 Answers

Use strtotime()

echo date('d-m-Y', strtotime("+1 week")); //1 week in the future
echo date('d-m-Y', strtotime("-1 week")); //1 week ago
like image 155
JohnP Avatar answered Oct 06 '22 02:10

JohnP


You can use the DateTime class to do calendar calculations. For exaple, to add one week, you could use code like this:

$date = new DateTime('22-05-2011');
$date->modify('+1 week');
like image 22
Lukáš Lalinský Avatar answered Oct 06 '22 04:10

Lukáš Lalinský


strtotime will handle this.

$pDate = strtotime('22-05-2011 + 1 week');
echo date('d-m-Y',$pDate);

Added: This is if you want to start from a specific date. If you just want 'today' +/- a week', mark JohnP's answer as correct. : )

like image 37
John Green Avatar answered Oct 06 '22 02:10

John Green