I
I
Issue2021-06-21 12:34:00
PHP
Issue, 2021-06-21 12:34:00

How to sort an array of dates and times in ascending order in php?

There are rows in the database with tasks and date/time timestamp ( like this 2021-06-20 14:30:00)
Using foreach I can iterate over the entire array, but I can't find the answer to the question of how to create an array like:

$calendar = [
    2021 => [
      6 => [
        20 => [
          1 => [
            'name' => 'taskname 1',
            'time' => '14:30:00',
          ],
          2 => [
            'name' => 'taskname 2',
            'time' => '15:00:00',
          ],
        ],
      ],
    ],
  ];


Hidden content is generated first, and after clicking on a certain day, it should display tasks from 00:00 to 23:59. Please tell me how to implement this using php.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wagoodoogoo, 2021-07-01
@wagoodooogoo

$array = [
  ['name' => 'taskname 1', 'date' => '2021-06-20 14:30:00'],
  ['name' => 'taskname 2', 'date' => '2021-06-21 14:30:00'],
  ['name' => 'taskname 3', 'date' => '2021-06-20 16:30:00'],
  ];

$calendar = [];
foreach ($array as $row) {
  list($date, $time) = explode(" ", $row['date']);
  list($year, $month, $day) = explode("-", $date);
  $calendar[$year][$month][$day][] = [$row['name'] , $time];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question