Is there an elegant way to create two date variables in php or mySQL. Essentially I have a query that needs to be run covering 1st October to 30th September the following year, so right now it would be 1st October 2022 to 30th September 2023, obviously once into October the date range become 1st October 2023 to 30th September 2024 and so on each year. played getting the individual year/month/day with the date() function and munging two date variables from that conditional on the current year but its seems very clumsy and inelegant
4
Answers
This might be what you’re looking for
You don’t need "Sept 30 and Oct 1", you need Oct 1 00:00:00 in successive years so you can check if dates are before or after that. Use DateTime and DateInterval for this, eg:
Output:
MySQL:
Use the
DATE_FORMAT
andDATE_ADD
functions to create the date range from October 1st of the current year to September 30thUse the
start_date
andend_date
in a SQL query to filter rows in a tablePHP: Demo – https://3v4l.org/Ch3rQ
For a pure MySQL approach you could do:
And you can test with:
Outputs:
Or:
Outputs: