Home > front end >  PHP - reading date from json and modifying as date not string
PHP - reading date from json and modifying as date not string

Time:01-28

I have a json file that is just a date - current_date.json:

["2022-01-28"]

I then try to read this date with PHP like so:

$string = file_get_contents("current_date.json");
$json = json_decode($string,true);

$end = date($json[0]);
$begin = date($end, strtotime("-1 days"));

This gives my $end variable as a string not a date and the $begin variable is not modified it is just the same string.

What is the best way to get these variables as dates?

The reason I am doing it like this is I want to work backwards one day at a time at which point I will overwrite the date in the json file so the next time this file runs it will get the previous day.

CodePudding user response:

let $begin value like this

$begin = date('Y-m-d',strtotime($end."-1 days")); // 2022-01-27
  •  Tags:  
  • Related