将日期分组为一年中的几周,PHP
Group dates into weeks of the year, PHP
我希望在 Wordpress 中显示事件(自定义 post 类型)这很容易做到,因为它们可以从最近到最远的时间排序。
但是,当我想显示“W/C April 19”时,它变得很复杂。
例如,每周基本上都会有一个新的 'events-row',其中包含该周的活动。
这我完全没有知道如何做的线索,我目前正在使用 foreach 循环来显示事件,而不是 WP_Query。
$query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => 6,
'order' => 'DESC'
) );
$posts = $query->posts;
if( $posts ){
$groupByWeek = array(); foreach( $posts as $post ){
setup_postdata( $post );
$eventDateTime = get_field('event_date_time', $post->ID); //Get the Start Date & Time Group
$eventStart = $eventDateTime['event_date_time_start']; //Get the event Start Date & Time
$date = DateTime::createFromFormat('d/m/Y g:i a', $eventStart); //Turn string into PHP DateTime
$firstDayOfWeek = 1; //Define the first day of the week in PHP 'N' format
$difference = ($firstDayOfWeek - $date->format('N')); //Find the difference between the first day and events day
if ($difference > 1) { //If the difference is greater than 1
$difference -= 7; //Take away 7 (1 week)
}
$date->modify("$difference days");
$week = $date->format('W');
if(!isset($groupByWeek[$week])){
$groupByWeek[$week] = [];
}
$groupByWeek[$week][] = $post;
}
}
$groupByWeek 转储:
array(1) {
[21]=> array(4) {
[0]=> object(WP_Post)#13970 (24) {
["ID"]=> int(625)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2021-04-22 19:56:50"
["post_date_gmt"]=> string(19) "2021-04-22 18:56:50"
["post_content"]=> string(233) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
["post_title"]=> string(69) "Events Name that likely goes over two lines but lets cater for three!"
}
}
[20]=> array(1) {
[0]=> object(WP_Post)#14271 (24) {
["ID"]=> int(635)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2021-04-22 19:55:08"
["post_date_gmt"]=> string(19) "2021-04-22 18:55:08"
["post_content"]=> string(233) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
["post_title"]=> string(69) "Events Name that likely goes over two lines but lets cater for three!"
}
}
}
所以上面的代码正确地将事件移动到它们自己的 Weekly 数组中,现在我必须遍历这个新数组以在前端显示事件以及周开始编号。我目前停留在哪个位置。
期望的结果:
<div class="all-events events-container">
<ul class="all-events-inner events-row">
<li class="week-commencing">W/C Apr 19</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
</ul>
<ul class="all-events-inner events-row">
<li class="week-commencing">W/C Apr 26</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
</ul>
<ul class="all-events-inner events-row">
<li class="week-commencing">W/C May 03</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
</ul>
</div>
我不认为你需要太多的代码来做到这一点,你只需要找到当前 post 是否属于特定的一周(一年中的第几周),然后创建一个基于此显示附加标记的小逻辑。
if ($posts) : ?>
<div class="all-events events-container">
<ul class="all-events-inner events-row">
<?php
// This will save the week that is being displayed
$current_week = false;
?>
<?php foreach ($posts as $post) : setup_postdata($post) ?>
<?php
// Get event start date
$start_date = get_post_meta(get_the_ID(), 'event_date_time_start', true);
// Find out this event week
$week = date('WY', strtotime($start_date));
// Check if this is the first time we are handling this week
if ($week !== $current_week) {
// Store the current week so that we don't show the same output once again until next week
$current_week = $week;
// Get the first day of this week
$dateTime = new DateTime(date("Y-m-d", strtotime($start_date)));
$monday = $dateTime->modify('Monday this week');
?>
<li class="week-commencing">W/C <?php echo $monday->format('M d'); ?></li>
<?php
}
?>
<li class="col-4 event">Blah blah stuff goes here</li>
<?php endforeach; ?>
</ul>
</div>
<?php wp_reset_postdata();
endif;
我希望在 Wordpress 中显示事件(自定义 post 类型)这很容易做到,因为它们可以从最近到最远的时间排序。
但是,当我想显示“W/C April 19”时,它变得很复杂。
例如,每周基本上都会有一个新的 'events-row',其中包含该周的活动。
这我完全没有知道如何做的线索,我目前正在使用 foreach 循环来显示事件,而不是 WP_Query。
$query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => 6,
'order' => 'DESC'
) );
$posts = $query->posts;
if( $posts ){
$groupByWeek = array(); foreach( $posts as $post ){
setup_postdata( $post );
$eventDateTime = get_field('event_date_time', $post->ID); //Get the Start Date & Time Group
$eventStart = $eventDateTime['event_date_time_start']; //Get the event Start Date & Time
$date = DateTime::createFromFormat('d/m/Y g:i a', $eventStart); //Turn string into PHP DateTime
$firstDayOfWeek = 1; //Define the first day of the week in PHP 'N' format
$difference = ($firstDayOfWeek - $date->format('N')); //Find the difference between the first day and events day
if ($difference > 1) { //If the difference is greater than 1
$difference -= 7; //Take away 7 (1 week)
}
$date->modify("$difference days");
$week = $date->format('W');
if(!isset($groupByWeek[$week])){
$groupByWeek[$week] = [];
}
$groupByWeek[$week][] = $post;
}
}
$groupByWeek 转储:
array(1) {
[21]=> array(4) {
[0]=> object(WP_Post)#13970 (24) {
["ID"]=> int(625)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2021-04-22 19:56:50"
["post_date_gmt"]=> string(19) "2021-04-22 18:56:50"
["post_content"]=> string(233) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
["post_title"]=> string(69) "Events Name that likely goes over two lines but lets cater for three!"
}
}
[20]=> array(1) {
[0]=> object(WP_Post)#14271 (24) {
["ID"]=> int(635)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2021-04-22 19:55:08"
["post_date_gmt"]=> string(19) "2021-04-22 18:55:08"
["post_content"]=> string(233) "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
["post_title"]=> string(69) "Events Name that likely goes over two lines but lets cater for three!"
}
}
}
所以上面的代码正确地将事件移动到它们自己的 Weekly 数组中,现在我必须遍历这个新数组以在前端显示事件以及周开始编号。我目前停留在哪个位置。
期望的结果:
<div class="all-events events-container">
<ul class="all-events-inner events-row">
<li class="week-commencing">W/C Apr 19</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
</ul>
<ul class="all-events-inner events-row">
<li class="week-commencing">W/C Apr 26</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
</ul>
<ul class="all-events-inner events-row">
<li class="week-commencing">W/C May 03</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
<li class="col-4 event">Blah blah stuff goes here</li>
</ul>
</div>
我不认为你需要太多的代码来做到这一点,你只需要找到当前 post 是否属于特定的一周(一年中的第几周),然后创建一个基于此显示附加标记的小逻辑。
if ($posts) : ?>
<div class="all-events events-container">
<ul class="all-events-inner events-row">
<?php
// This will save the week that is being displayed
$current_week = false;
?>
<?php foreach ($posts as $post) : setup_postdata($post) ?>
<?php
// Get event start date
$start_date = get_post_meta(get_the_ID(), 'event_date_time_start', true);
// Find out this event week
$week = date('WY', strtotime($start_date));
// Check if this is the first time we are handling this week
if ($week !== $current_week) {
// Store the current week so that we don't show the same output once again until next week
$current_week = $week;
// Get the first day of this week
$dateTime = new DateTime(date("Y-m-d", strtotime($start_date)));
$monday = $dateTime->modify('Monday this week');
?>
<li class="week-commencing">W/C <?php echo $monday->format('M d'); ?></li>
<?php
}
?>
<li class="col-4 event">Blah blah stuff goes here</li>
<?php endforeach; ?>
</ul>
</div>
<?php wp_reset_postdata();
endif;