1

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
  • 15,271
  • 5
  • 39
  • 49
Mike Pala
  • 766
  • 1
  • 11
  • 39

3 Answers3

3

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
  • 72
  • 1
  • 1
  • 6
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,972
  • 1
  • 14
  • 34
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
  • 15,028
  • 4
  • 24
  • 42