Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

axtg's avatar
Level 5

Find date in range

I am preparing data to go into a graph rendered in JS. What I want to do is draw two data sets of an identical period (e.g. 7 days, 30 days) on one x-axis.

This is the approach I identified:

  1. Determine the length of the date range selected, e.g.
use Carbon\CarbonPeriod;
$range = CarbonPeriod::create(Carbon::today()->subWeek(), Carbon::today())->count();
  1. Load the array of events in that time range, imagine that would return:
use Carbon\Carbon;
$data = [Carbon::today()->subDays(2),Carbon::today()->subDays(5)];
  1. Determine what number day 1 and 2 are in $data within $range.

Tltr: I imagine I can then plot the numbers from 3 on the full x-axis range ($range). And by doing the same for dataset two, I will have identical measures to be able to plot both series on one x-axis. Happy to hear other approaches. What I basically want to do is (using Apexcharts) have this:

Google Analytics date compare

0 likes
6 replies
axtg's avatar
Level 5

I can make it shorter :).

How can I create

  1. One consistent array with a range (x-axis) of dates (e.g. $range = [0] Jan 1 - [6] Jan 7), and
  2. Create new arrays with values at the appropriate $range key (e.g. $values = [[X, 100], [Y, 200]] where X and Y are the keys from $range.

End goal: be able to plot the values from the arrays in 2. on the x-axis determined by 1.

If that does not make any sense (don't hold back), I'll give the entire concept a rethink.

RamjithAp's avatar

Try this

$range = [];
$values = [];
for($i=0;$i<6;$i++){
    //Get $date from carbon 
    $range[]= $date->format("any format");
    $values[] = // use $date to query and get the result count 
}
bugsysha's avatar

One consistent array with a range (x-axis) of dates (e.g. $range = [0] Jan 1 - [6] Jan 7)

collect(range(1, 6))->map(function (int $value): string {
  return now()->addDays($value)->format('Y-m-d');
});

// PHP7.4+
collect(range(1, 6))->map(fn (int $value): string => now()->addDays($value)->format('Y-m-d'));

Create new arrays with values at the appropriate $range key (e.g. $values = [[X, 100], [Y, 200]] where X and Y are the keys from $range.

Keys from range?

axtg's avatar
Level 5

Okay, I fiddled a bit more, maybe my latest understanding helps word it more properly (embarrassing how bad I am at this...) Thanks for sticking with me!

I'm stopping with the textual descriptions. This is a piece of code that does what I need now. But it is ugly and seems very inefficient.

        $datesA = ['4 May', '5 May'];
        $datesB = ['27 April', '28 April'];
        $eventsA = [['5 May', 100]];
        $eventsB = [['27 April', 50], ['28 April', 200]];
        $seriesA = [];
        $seriesB = [];

        foreach ($eventsA as $key => $value) {
            $seriesA[] = (array_search($value[0], $datesA) ? [$value[0], $value[1]] : null);
        }

        foreach ($eventsB as $key => $value) {
             $seriesB[] = (array_search($value[0], $datesB) ? [$value[0], $value[1]] : null);
        }

Now I can plot $seriesA and $seriesB on one x-axis as their key is now directly comparable. 4 May and 27 April are not, 0 and 0 are.

Still very open to do this cleaner!

bugsysha's avatar

So this code works? If so then encapsulate it into some class, write tests for it and only then refactor it.

Please or to participate in this conversation.