I am working on a sales schedule. I have an X axis. To the top there is a filter on the page that allows you to enter a date range, that is, first enter the date from, then the date to.
I need to determine how to display dates on the X axis.
I suggest doing it like this:
For example, if a person entered 2 dates in the limit of one month, or between months, but no more than say 31 days, then I need to get an array, where there will be elements by days.
That is, the X axis will be: 10/01/2017, 10/02/2017, ... 10/31/2017.
But if the user entered 2 dates in the range greater than 31, then it is necessary to output an array of 2 dates. The first element is the first month, the second is the second.
In this case, the X axis should be 10.2017, 12.2017
If a person has entered a range of more than 12 months, then it is already necessary to output for years.
In this case, the X axis should be 2016, 2017
Counting the number of days between two dates, I do this:
$datetime1 = date_create("01.05.2017"); $datetime2 = date_create("31.05.2017"); $interval = date_diff($datetime1, $datetime2); $days = $interval->format('%a'); if($days <= 31) { $period = new DatePeriod( new DateTime($dateFrom), new DateInterval('P1D'), (new DateTime($dateTo))->modify('+1 days') ); //Абсцисса Х $daysArray; foreach( $period as $date) { $daysArray .= "'".$date->format('Ym-d')."',"; } } if($days > 31 && $days < 365) { //Тут уже необходимо выводить не дни а месяца в формате mY $period = new DatePeriod( new DateTime($dateFrom), new DateInterval('P1D'), (new DateTime($dateTo))->modify('+1 month') ); //Абсцисса Х $daysArray; foreach( $period as $date) { $daysArray .= "'".$date->format('Ym-d')."',"; } } After echo $ daysArray, I get what I need. But my code works only in the aisles of 31 days, if I add a condition> 31 days, then I already need to display only the dates by months. I am by analogy with <31, but in modify I put +1 month but as a result I get by day, not by month.
The question is how to implement it?
DatePeriodonly at intervals of months and years, respectively - Alexey Shimansky