How to find the first day of current month in phpAsk a question
I need to aggregate all data in MySQL table which is older than the current month. Expect the current month. So now I should find all records that are older than the first day of the current month. I use
$now = new \DateTime() which returns . the current date and now I need to replace the day with 1 or 01. So what is the best way to do it?
I have found a very nice way to do it. It works with PHP 7.1 I guess with other versions as well
$startOfCurrentMonth = new \DateTime('first day of this month');
BTW it also works for the last day
$endOfCurrentMonth = new \DateTime('last day of this month');
in case if month and year is geven than in order to find the first and end date of that month the following code will help
$date = ['y' => 2019, 'm' => 4]; $monthStartDate = new \DateTime(); $monthStartDate->setDate($date['y'], $date['m'], 1); $monthEndDate = new \DateTime(); $monthEndDate->setDate($date['y'], $date['m'], 1); $monthEndDate->modify('last day of this month');
- 2How to move logging from PHP-FPM to Nginx
- 1How to disable php error reporting on production server
- 1How to change PHP CLI and Apache versions on Ubuntu
- 0How to show whole month in calendar?
- 0how to find next element of current element?
- 0Regex loops continuously without terminating to find a match
- 0Where I can find the best templates around Bootstrap cards?
- 1How to skip first n lines of grep result
- 1How to prevent SQL injections in PHP
- 1How do I retrieve all 60 years old above using birth_date in PHP PDO?
- 0How to group by year and month in My SQL