From c2354e7397f44998691dfe8241e49c5a65f66dcc Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 28 Jan 2023 17:38:32 +0100 Subject: Use DateTimeImmutable for concert date/time. --- includes/class-giglogadmin-concert.php | 12 ++++++------ includes/giglogadmin-export-ical-handler.php | 11 ++--------- includes/view-helpers/class-giglogadmin-concertform.php | 2 +- includes/view-helpers/class-giglogadmin-concertstable.php | 2 +- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/includes/class-giglogadmin-concert.php b/includes/class-giglogadmin-concert.php index 6c7c4da..f669c03 100644 --- a/includes/class-giglogadmin-concert.php +++ b/includes/class-giglogadmin-concert.php @@ -21,7 +21,7 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { private ?int $id; private ?string $cname; private ?GiglogAdmin_Venue $venue; - private ?string $cdate; + private DateTimeImmutable $cdate; private ?string $tickets; private ?string $eventlink; private ?int $status; @@ -50,7 +50,7 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { public function __construct( object $attrs ) { $this->id = isset( $attrs->id ) ? $attrs->id : null; $this->cname = isset( $attrs->wpgconcert_name ) ? $attrs->wpgconcert_name : null; - $this->cdate = isset( $attrs->wpgconcert_date ) ? $attrs->wpgconcert_date : null; + $this->cdate = new DateTimeImmutable( $attrs->wpgconcert_date ?? 'now' ); $this->tickets = isset( $attrs->wpgconcert_tickets ) ? $attrs->wpgconcert_tickets : null; $this->eventlink = isset( $attrs->wpgconcert_event ) ? $attrs->wpgconcert_event : null; $this->status = isset( $attrs->wpgconcert_status ) ? $attrs->wpgconcert_status : 0; @@ -141,7 +141,7 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { } if ( isset( $attrs->wpgconcert_date ) && $attrs->wpgconcert_date != $this->cdate ) { - $this->cdate = $attrs->wpgconcert_date; + $this->cdate = new DateTimeImmutable( $attrs->wpgconcert_date ); $need_update = true; } @@ -305,7 +305,7 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { $columns = array( 'wpgconcert_name' => $this->cname, 'venue' => $this->venue->id(), - 'wpgconcert_date' => $this->cdate, + 'wpgconcert_date' => $this->cdate->format( 'c' ), 'wpgconcert_tickets' => $this->tickets, 'wpgconcert_event' => $this->eventlink, 'wpgconcert_status' => $this->status, @@ -349,8 +349,8 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { /** * Return the date of the concert. */ - public function cdate() { - return $this->cdate ? $this->cdate : ''; + public function cdate() : DateTimeImmutable { + return $this->cdate; } /** diff --git a/includes/giglogadmin-export-ical-handler.php b/includes/giglogadmin-export-ical-handler.php index 44afbea..c230638 100644 --- a/includes/giglogadmin-export-ical-handler.php +++ b/includes/giglogadmin-export-ical-handler.php @@ -30,11 +30,9 @@ if ( ! function_exists( 'giglogadmin_export_ical_handler' ) ) { $cfullname = $concert->cname() . ' live at ' . $concert->venue()->name() . ', ' . $concert->venue()->city(); $cshortname = substr( $cfullname, 0, 20 ); - $fdate = strtotime( $concert->cdate() ); - $newformat = date( 'Ymd', $fdate ); $filename = sanitize_file_name( - "Concert-{$concert->venue()->name()}-{$newformat}-" + "Concert-{$concert->venue()->name()}-{$concert->cdate()->format( 'Ymd' )}-" . substr( $concert->cname(), 0, 30 ) ); @@ -58,12 +56,7 @@ if ( ! function_exists( 'giglogadmin_export_ical_handler' ) ) { // place the event ->setLocation( "{$concert->venue()->name()}, {$concert->venue()->city()}" ) // set the time - ->setDtstart( - new DateTime( - $newformat . 'T190000', - new DateTimezone( 'Europe/Oslo' ) - ) - ) + ->setDtstart( $concert->cdate() ) ->setDuration( 'PT4H' ); header( 'Content-Type: text/calendar' ); diff --git a/includes/view-helpers/class-giglogadmin-concertform.php b/includes/view-helpers/class-giglogadmin-concertform.php index 5cc3a47..cf7a466 100644 --- a/includes/view-helpers/class-giglogadmin-concertform.php +++ b/includes/view-helpers/class-giglogadmin-concertform.php @@ -66,7 +66,7 @@ if ( ! class_exists( 'GiglogAdmin_ConcertForm' ) ) { . '' . $this->get_venue_selector( $c->venue() ) . '
' // date has to be formatted else it is not red in the date field of html form . '' - . '
' + . '
' . '' . '
' . '' diff --git a/includes/view-helpers/class-giglogadmin-concertstable.php b/includes/view-helpers/class-giglogadmin-concertstable.php index 1288cbd..9bf848c 100644 --- a/includes/view-helpers/class-giglogadmin-concertstable.php +++ b/includes/view-helpers/class-giglogadmin-concertstable.php @@ -291,7 +291,7 @@ if ( ! class_exists( 'GiglogAdmin_ConcertsTable' ) ) { $content .= ''; $content .= - '' . date( 'd.M.Y', strtotime( $concert->cdate() ) ) . '' + '' . $concert->cdate()->format( 'd.m.Y' ) . '' . '' . strtoupper( esc_html( $concert->cname() ) ) . '' . '' . esc_html( $concert->venue()->name() ) . ''; -- cgit v1.2.3