Skip to content

Commit

Permalink
Merge pull request #111 from WordPress/modify-list
Browse files Browse the repository at this point in the history
Modify how date is displayed on the event list
  • Loading branch information
trymebytes authored Feb 22, 2024
2 parents 926390c + 9a0c237 commit 8fa700c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
25 changes: 25 additions & 0 deletions includes/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,29 @@ public function end(): DateTimeImmutable {
public function timezone(): DateTimeZone {
return $this->timezone;
}

/**
* Generate text for the end date.
*
* @param string $event_end The end date.
*
* @return string The end date text.
*/
public static function get_end_date_text( string $event_end ): string {
$end_date_time = new DateTimeImmutable( $event_end );
$current_date_time = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );

$interval = $end_date_time->diff( $current_date_time );
$hours_left = ( $interval->d * 24 ) + $interval->h;
$hours_in_a_day = 24;

if ( 0 === $hours_left ) {
/* translators: %s: Number of minutes left. */
return sprintf( _n( 'ends in %s minute', 'ends in %s minutes', $interval->i ), $interval->i );
} elseif ( $hours_left <= $hours_in_a_day ) {
/* translators: %s: Number of hours left. */
return sprintf( _n( 'ends in %s hour', 'ends in %s hours', $hours_left ), $hours_left );
}
return sprintf( 'until %s', $end_date_time->format( 'M j, Y' ) );
}
}
14 changes: 7 additions & 7 deletions templates/events-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
<?php
while ( $current_events_query->have_posts() ) :
$current_events_query->the_post();
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format( 'l, F j, Y' );
$event_url = gp_url( wp_make_link_relative( get_the_permalink() ) );
$event_end = Event::get_end_date_text( get_post_meta( get_the_ID(), '_event_end', true ) );
$event_url = gp_url( wp_make_link_relative( get_the_permalink() ) );
?>
<li class="event-list-item">
<span class="event-list-date"><?php echo esc_html( $event_start ); ?></span>
<a href="<?php echo esc_url( $event_url ); ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<p><?php the_excerpt(); ?></p>
<a href="<?php echo esc_url( $event_url ); ?>"><?php the_title(); ?></a>
<span class="event-list-date"><?php echo esc_html( $event_end ); ?></span>
<?php the_excerpt(); ?>
</li>
<?php
endwhile;
Expand Down Expand Up @@ -65,9 +65,9 @@
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format( 'l, F j, Y' );
?>
<li class="event-list-item">
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ); ?>"><?php the_title(); ?></a>
<span class="event-list-date"><?php echo esc_html( $event_start ); ?></span>
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ); ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<p><?php the_excerpt(); ?></p>
<?php the_excerpt(); ?>
</li>
<?php
endwhile;
Expand Down

0 comments on commit 8fa700c

Please sign in to comment.