0

I have a php variable:

$datevar = date("Y-m-d");

Which shows me the CURRENT date in the specified format.

What I want is the date from 7 days ago for CURRENT date in that format. I have tried:

$datevar = $datevar - 7; 

and

$datevar = date("Y-m-d") - 7 ;

But they both cause an error.

aorcsik
  • 14,571
  • 4
  • 38
  • 49
Mike Pala
  • 696
  • 1
  • 10
  • 31

3 Answers3

2

Try the code below, that should work:

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

http://php.net/manual/en/function.strtotime.php

Twinfriends
  • 1,942
  • 1
  • 12
  • 32
2

You can do it like this. Here we are using DateTime and DateInterval

Try this code snippet here

<?php

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

$dateTimeObj=new DateTime($datevar);
$dateTimeObj->sub(new DateInterval("P7D"));//subtracting 7 days
echo $dateTimeObj->format("Y-m-d");
Sahil Gulati
  • 14,792
  • 4
  • 23
  • 42
2

I refer to this solution: https://stackoverflow.com/a/3727821/7454754

So in your example this would be something like

<?php
  $today = date("Y-m-d");
  $newDate = date("Y-m-d", strtotime($today . " - 7 days"));
?>
Marcel Goldammer
  • 62
  • 1
  • 1
  • 6