Answer the question
In order to leave comments, you need to log in
How to solve the riddle with the date?
They gave me a project to lead, and there the date works through the DateTime (php) library. There is such a mechanism - if the date is transmitted through the get, then the date object is formed from it, otherwise the date is the current day.
$date = $request->getParam('date');
if (!empty($date)) {
$date = DateTime::createFromFormat('Y-m-d', $date);
} else {
$date = new DateTime;
}
$my_date = strtotime($date->date);
$my_date = strtotime('-1 day', $my_date);
$my_date = date('Y-m-d', $my_date);
DateTime Object ( [date] => 2018-04-01 15:30:00.000000 [timezone_type] => 3 [timezone] => Asia/Omsk )
1969-12-31
2018-03-31
Answer the question
In order to leave comments, you need to log in
Have you tried that?
/** @var \DateTime $date */
$date->modify('-1 day');
Create a date immediately "yesterday"
///...
} else {
$date = new DateTime('yesterday');
}
First, it's a very bad idea to use the same variable for both string and object.
Secondly, it is necessary to check the correctness of the date. If it came, but not in the format, then an object with a zero date (the beginning of the unix epoch, that is, 1970-01-01) may be created
. Third, specify a test-case with your problem. So that you can run it for yourself to check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question