0

I have a WordPress site and I am using a plugin to manage registrations to events (the plugin name is Events Manager Pro).

With this plugin, we are allowed to create placeholders to display various information on events webpages.

I created a placeholder to show the date and time when registrations to an event starts. The name I gave to my placeholder is: #_REGISTRATIONSTARTDATETIME

I use this code in the function.php file of my child theme:

function my_em_registrationstartdatetime_placeholder($replace, $EM_Event, $result){
    if ( $result == '#_REGISTRATIONSTARTDATETIME' ) {
        $ticket_list = "";
        foreach( $EM_Event->get_tickets()->tickets as $EM_Ticket ){
          $ticket_list .= $EM_Ticket->get_datetime('start');
        }
        $replace = $ticket_list;
    }
    return $replace;
}
add_filter('em_event_output_placeholder','my_em_registrationstartdatetime_placeholder',1,3);

My problem is the date and time is output in this format: 2023-01-09 10:00:00

I wish to have the date and time output in this format instead: Monday January 9 at 10h00

Thank you!

My knowledge in PHP is limited. I am sorry, but I am eager to learn. Does anyone know what could be modified in my PHP code to format the date and time?

3

2 Answers 2

1

Thank you. It now works.

Here is the code I have in my functions.php file :

function my_em_registrationstartdatetime_placeholder($replace, $EM_Event, $result){
    if ( $result == '#_REGISTRATIONSTARTDATETIME' ) {
        $ticket_list = "";
        foreach( $EM_Event->get_tickets()->tickets as $EM_Ticket ){
          $ticket_list = $EM_Ticket->get_datetime('start');
        }
        $replace = wp_date('l j F \a\t G\hi', wp_strtotime($ticket_list));
    }
    return $replace;
}
add_filter('em_event_output_placeholder','my_em_registrationstartdatetime_placeholder',1,3);

In the functions.php file, I also added the code recommended in the link suggested by Moishy : https://mediarealm.com.au/articles/wordpress-timezones-strtotime-date-functions/.

Sign up to request clarification or add additional context in comments.

Comments

0

use wp_date function and php date format parameters

$replace = wp_date('l F j \a\t G\hi', strtotime($ticket_list));

to add a timezone to it, add a DateTimeZone argument to the function. For example:

$replace = wp_date('l F j \a\t G\hi', strtotime($ticket_list), new DateTimeZone('America/Vancouver'));

5 Comments

It works with $replace = wp_date('l F j \a\t G\hi', strtotime($ticket_list));. But, there is one last thing. The time is off by 4 hours. I guess there is somewhere I need to mention the timezone?
updated my answer with timezone control
by default wp_date() calls the timezone set in Settings > General using the wp_timezone() function
There is something strange. In WordPress in Settings > General, I have New York as timezone. I am located in Montreal, Qc (Canada). Which is in straight vertical line with New York, NY. The plugin I am using (Events Manager Pro) has also a setting for timezone and it is set to New York. I use $replace = wp_date('l F j \a\t G\hi', strtotime($ticket_list), new DateTimeZone('America/New_York'));. I get a delay of 4 hours. If I reverse back to $replace = $ticket_list;, the time is correct, but the format goes back to 2023-01-09 10:00:00. Which is the right time, but not the right format.
might be the strtotime function playing up. check out this link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.