Сортировка php объекта, по ближайшими датами
У меня есть массив внутри которого есть объекты. Надо реализовать сортировку по дате начала ивента которая близка к сегодняшней дате. Вот пример объекта. Это wordpress object где можно брать через get_post_meta() дату начала ивента.
<pre>
array(4) {
[0]=>
object(WP_Post)#3813 (24) {
["ID"]=>
int(9905)
["post_author"]=>
string(2) "31"
["post_date"]=>
string(19) "2020-07-30 21:14:06"
["post_date_gmt"]=>
string(19) "2020-07-30 21:14:06"
["post_content"]=>
string(251) "ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad"
["post_title"]=>
string(20) "Test 3 same location"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(20) "test-3-same-location"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2020-07-30 21:15:32"
["post_modified_gmt"]=>
string(19) "2020-07-30 21:15:32"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(62) "https://www.stag.wediscover.club/listing/test-3-same-location/"
["menu_order"]=>
int(0)
["post_type"]=>
string(7) "listing"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[1]=>
object(WP_Post)#3812 (24) {
["ID"]=>
int(9903)
["post_author"]=>
string(2) "31"
["post_date"]=>
string(19) "2020-07-29 20:32:40"
["post_date_gmt"]=>
string(19) "2020-07-29 20:32:40"
["post_content"]=>
string(167) "Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events"
["post_title"]=>
string(10) "Location 2"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(10) "location-2"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2020-07-30 20:53:08"
["post_modified_gmt"]=>
string(19) "2020-07-30 20:53:08"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(52) "https://www.stag.wediscover.club/listing/location-2/"
["menu_order"]=>
int(0)
["post_type"]=>
string(7) "listing"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[2]=>
object(WP_Post)#3816 (24) {
["ID"]=>
int(9901)
["post_author"]=>
string(2) "31"
["post_date"]=>
string(19) "2020-07-29 20:31:20"
["post_date_gmt"]=>
string(19) "2020-07-29 20:31:20"
["post_content"]=>
string(158) "Location test 1 Location test 1 Location test 1 Location test 1 Location test 1 Location test 1Location test 1 Location test 1 Location test 1 Location test 1"
["post_title"]=>
string(15) "Location test 1"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(15) "location-test-1"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2020-07-30 20:20:56"
["post_modified_gmt"]=>
string(19) "2020-07-30 20:20:56"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(57) "https://www.stag.wediscover.club/listing/location-test-1/"
["menu_order"]=>
int(0)
["post_type"]=>
string(7) "listing"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
[3]=>
object(WP_Post)#3817 (24) {
["ID"]=>
int(9747)
["post_author"]=>
string(2) "29"
["post_date"]=>
string(19) "2020-07-02 21:24:09"
["post_date_gmt"]=>
string(19) "2020-07-02 21:24:09"
["post_content"]=>
string(525) "Join our most popular surfing meetup in Los Angeles.
Surfers with all experience levels welcome!
Approximate schedule for the morning:
8.45 – 9.00 Meeting next to Perry’s Cafe
9.00 – 9.30 Surfing
9.30 – 9.45 Coffe / Snacks
9.45 – 10.15 Surfing
10.15 – 10.30 Coffe/ Snacks
10.30 – 11.00 Volleyball
We advise you to bring hot coffee or tea with you to make this morning even more cozier after surfing)
Feel free to contact us if you have any questions.
We can’t wait to see you!"
["post_title"]=>
string(40) "Surfing and Volleyball at Torrance beach"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(42) "surfing-and-volleyball-at-torrance-beach-4"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2020-07-15 23:06:59"
["post_modified_gmt"]=>
string(19) "2020-07-15 23:06:59"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(84) "https://www.stag.wediscover.club/listing/surfing-and-volleyball-at-torrance-beach-3/"
["menu_order"]=>
int(0)
["post_type"]=>
string(7) "listing"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
}
</pre>
Ответы (1 шт):
Автор решения: Morgan Old
→ Ссылка
Решил задачу по такому формату. Может уверен, что лучшее решение есть, буду рад новым ответам.
$sortResult = [];
$keysArr = [];
$eventStDate = "";
$diffDays = 0;
$interval = 0;
$returningSortResult = $result;
foreach ($result->posts as $ppkey => $ppvalue) {
$eventStDate = get_post_meta($ppvalue->ID, "_event_date", false);
$interval = date_diff(new DateTime(date( "Y-m-d", strtotime($nowDate) )), new DateTime(date( "Y-m-d", strtotime($eventStDate[0]) )));
$sortResult[]["key"] = array($interval->d, $ppvalue);
}
asort($sortResult);
$theKey = 0;
foreach ($sortResult as $sortKey => $sortValue) {
$keysArr[]["key"] = $sortKey;
$returningSortResult->posts[$theKey] = $sortValue["key"][1];
$theKey++;
}
Если будут улучшения буду рад посмотреть. Спасибо!