S
S
Sergey750il2021-06-09 23:26:16
PHP
Sergey750il, 2021-06-09 23:26:16

How to sort an array by date?

Good time of day. How to sort an array by date?

$arr= [
    [
      "header"=>'',
      "title"=>'', 
      "data"=>'12 июнь 2021', 
    ],
    [
      "header"=>'',
      "title"=>'', 
      "data"=>'30 июнь 2021', 
    ],
    [
      "header"=>'',
      "title"=>'', 
      "data"=>'1 июля 2021', 
    ],
    [
      "header"=>'',
      "title"=>'', 
      "data"=>'1 январь 2020', 
    ]
  ];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-06-09
@Sergey750il

Use the usort function . For it, you will need to write a comparison callback function that will receive elements from your array.
In this callback function, you will need to translate the date of the form "July 1, 2021" either into a timestamp or into a DateTime object, and already use them in comparison with each other.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question