WordPress.org

Forums

Events Manager
[resolved] Display a date between 2 month (24 posts)

  1. Daniele
    Member
    Posted 1 year ago #

    Hi guys,

    i need help.

    How can i display a date that start at 31 may and end at 01 june?
    For example 31 aug 01 sept 2013 whereas when the event come in the middle month display 07-08 sept 2013

    http://wordpress.org/plugins/events-manager/

  2. angelo
    NetWebLogic Support
    Posted 1 year ago #

    you can try something like #j #M #Y #@_{ \u\n\t\i\l j M Y}

    http://wp-events-plugin.com/documentation/placeholders/

  3. Daniele
    Member
    Posted 1 year ago #

    Thanks, it works fine, but i need also that if is the same month, i won't display it twice like this:

    13 aug 2013 - 27 aug 2013

    correct form would be:
    13 - 27 aug 2013 or 31 aug 2013 - 01 sept 2013

    is possible to have this solution?

  4. Philip John
    Member
    Posted 1 year ago #

    Hiya,

    That isn't possible out of the box I'm afraid. You would need to creaet your own conditional placeholder to detect the month.

    See http://wp-events-plugin.com/tutorials/creating-conditional-placeholders-for-events/ for more details on how to do that.

    Cheers,
    Phil

  5. angelo
    NetWebLogic Support
    Posted 1 year ago #

    try this snippet - http://pastebin.com/LecuD8bp

    *paste in your theme functions.php

  6. Daniele
    Member
    Posted 1 year ago #

    Hi angelo_nwl,

    your snippet check if the date is more long than one day, so it is not useful for my purpose. I need to check if the month is the same or if it's different, like philipjohn said...

    Is maybe possible to check what is the month on the output of $EM_Event->event_start_date ?

  7. angelo
    NetWebLogic Support
    Posted 1 year ago #

    you can use that snippet as a starting point then add new conditions to achieve your objectives.

  8. Daedalon
    Member
    Posted 1 year ago #

    We wrestled with the same a year ago and had to create and maintain our own custom placeholder for this.

    Hopefully this capability will be added to EM so that you can specify a shortcode where a year, month, date or time will be output only if it's different from the start/end time. If it would be denoted with %, one could then have a shortcode like #j.#%_{n\.}#%Y#@_{ \- j\.n\.Y} with the following results:

    • If there's only a start date, it will be output as 20.5.2013.
    • If start and end date exist and their month and year are the same: 5. - 29.11.2013.
    • -"- month is different but year is the same: 5.11. - 5.12.2013
    • -"- month and year are different: 5.11.2013 - 5.1.2014

    #%Y is interpreted and printed only when different from the opposing (end) date.

    #%_{} characters between the {} are interpreted and printed only when different from the opposing (end) date. If the only characters that would be printed are escaped literals such as \., nothing is printed.

    As an important side note, something should be considered different from the opposing (end) date always when it (eg. month) is different OR when anything larger than it (eg. year) is different. This way we won't have an event that lasts one year being output as 5.2013 - 5.11.2014 without the month. When checking for the month, it should be treated as different because the year is different.

  9. Daniele
    Member
    Posted 1 year ago #

    Thanks for explanation.

    I'm not an expert developer, so can you tell how create a shortcode or a placeholder with these comparison criteria?

  10. Daedalon
    Member
    Posted 1 year ago #

    Everything from the second paragraph onwards was just a suggestion on how the plugin could be developed in the future.

    To add a custom placeholder you should make a custom plugin, or as a possible workaround, just use your theme's functions.php (by pasting any new PHP code there).

    What we use is:

    add_filter( 'em_event_output_placeholder', 'my_custom_event_placeholders', 1, 3);
    /**
    * Custom Events Manager placeholders
    * @param string $result
    * @param EM_Event $EM_Event
    * @param string $placeholder
    * @return string
    */
    function my_custom_event_placeholders( $replace, $EM_Event, $result ) {
        global $wp_query, $wp_rewrite;
        switch ( $result ) {
    		case '#_CUSTOMDATEPLACEHOLDERNAME':
    			$replace = '';
    			$start_date = strtotime( $EM_Event->event_start_date );
    			$end_date = strtotime( $EM_Event->event_end_date );
    			$replace = date_i18n( 'D j.n.Y', $start_date );
    			$timediff = $end_date - $start_date;
    			if ( $timediff > 86400 || ( $timediff == 86400 && $EM_Event->event_start_time <= $EM_Event->event_end_time ) ) {
    				// lasts 24h or longer, print end date
    				$replace .= ' - ' . date_i18n( 'D j.n.Y', $end_date );
    			}
    //			$replace = __( apply_filters( 'em_event_output_placeholder', $replace ) );
    			break;
    	}
    	return $replace;
    }

    Now wherever you use #_CUSTOMDATEPLACEHOLDERNAME, you get the same output as with #_EVENTDATES with one exception: the end date is printed only of the beginning and end times are more than 24 hours apart. For example an event from 10:00 to 11:00 on a single day or an event from 18:00 to 02:00 would only show the start date.

    This uses a predefined time format D j.n.Y that can be changed only directly at the code. Another option is replacing 'D j.n.Y' with eg. get_option('dbem_date_format') to use the one value of Events > Settings > Formatting > Date and time > Date formatting. As we wanted to have one value for that to use in eg. event lists and then another one on the individual event pages, we had to use a fixed setting.

  11. Daedalon
    Member
    Posted 1 year ago #

    EM devs: the example in http://wp-events-plugin.com/tutorials/creating-conditional-placeholders-for-events/ could be expanded. It took us some pondering to figure out how to do the above, especially as copying a core placeholder and modifying it didn't work directly (references to this needed to be changed to $EM_Event for events, and different for locations and categories). Explaining those up-front could go a long way especially for siteadmins who don't have test sites and would crash their main site with a code that incorrectly references this.

  12. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    where do you see references to $this?

  13. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    actually, pls post on a new thread as this'll go off-topic

  14. Daniele
    Member
    Posted 1 year ago #

    I have used the code above and it works fine, but only in local...

    This is the code modified by me:

    add_filter( 'em_event_output_placeholder', 'my_custom_event_placeholders', 1, 3);
    /**
    * Custom Events Manager placeholders
    * @param string $result
    * @param EM_Event $EM_Event
    * @param string $placeholder
    * @return string
    */
    function my_custom_event_placeholders( $replace, $EM_Event, $result ) {
        switch ( $result ) {
    		case '#_CUSTOMEVENTDATES':
    			$replace = '';
    			$start_date = strtotime( $EM_Event->event_start_date );
    			$end_date = strtotime( $EM_Event->event_end_date );
    			$timediff = $end_date - $start_date;
    
    			// Check if the number of the day is different
    			if ( $timediff >= 86400 ) {
    
    				// Check if is the same month
    				if ( date_i18n( 'M', $end_date ) == date_i18n( 'M', $start_date ) ) {
    
    					// Print only the start day
    					$replace = date_i18n( 'j', $start_date ). ' - ' . date_i18n( 'j M Y', $end_date );
    
    				}else{
    
    					// Check if is the same year
    					if ( date_i18n( 'Y', $end_date ) == date_i18n( 'Y', $start_date ) ) {
    
    						// Print only the start day and month
    						$replace = date_i18n( 'j M', $start_date ). ' - ' . date_i18n( 'j M Y', $end_date );
    
    					}else{
    
    						// Print the complete start and end date
    						$replace = date_i18n( 'j M Y', $start_date ). ' - ' . date_i18n( 'j M Y', $end_date );
    
    					}	
    
    				}
    
    			}else{
    
    				$replace = date_i18n( 'j M Y', $start_date );
    
    			}
    
    			$replace = __( apply_filters( 'em_event_output_placeholder', $replace ) );
    			break;
    	}
    	return $replace;
    }

    when i use this code on functions.php in the online site, it gives me this error:

    Warning: Missing argument 2 for em_event_output_placeholder() in .../wp-content/plugins/events-manager/classes/em-event.php on line 2144

    Warning: Missing argument 3 for em_event_output_placeholder() in .../wp-content/plugins/events-manager/classes/em-event.php on line 2144

    Warning: Missing argument 2 for my_custom_event_placeholders() in .../wp-content/themes/vita_e_crescita/functions.php on line 722

    Warning: Missing argument 3 for my_custom_event_placeholders() in .../wp-content/themes/vita_e_crescita/functions.php on line 722

    Where am i wrong?

  15. Daedalon
    Member
    Posted 1 year ago #

    Daniele, in that case you may need to create a custom plugin instead. You could try the instruction I've outlined in http://wordpress.org/support/topic/expanding-the-tutorial-for-creating-conditional-placeholders:

    1. Create a folder: wp-content/plugins/my-em-custom
    2. Create a file: wp-content/plugins/my-em-custom/my-em-custom.php
    3. Copy-paste this content to that file and save it: <content>
    4. Go to WP admin panel and enable the plugin

    In this case <content> would be these lines first and the above code after:

    <?php
    /*
    Plugin Name: Events Manager customizations for My Site
    Version: 0.1
    Description: Custom scopes and other customizations to Events Manager for My Site
    Author: Me
    */

    If any plugin customizations you make break the whole site including wp-admin, you can disable the plugin by renaming its folder or completely remove it (make a backup first if it's your own code!). This works for any plugin or theme causing issues.

  16. Daedalon
    Member
    Posted 1 year ago #

    Daniele, I'm replying here to your message at http://wordpress.org/support/topic/expanding-the-tutorial-for-creating-conditional-placeholders?replies=2#post-4457996 to keep the messages related to solving your issue in one thread :)

    To narrow down the cause for your warnings, you can try disabling certain parts of the code. To disable one line, add // to the beginning of it, and to disable many mark the start of the disabled code with /* and the end with */. Those symbols mark something as a comment instead of actual code that would be executed.

    To clarify, do you get any warnings without this piece of code? When you get the warnings, where exactly do you get them? Is there a placeholder used where the errors occur? Which one? Do other placeholders work normally?

  17. Daniele
    Member
    Posted 1 year ago #

    Hi,

    thanks for your patience. I'll try to follow your suggestions and I'll keep you update :)

  18. Daniele
    Member
    Posted 1 year ago #

    I found the implied piece of code that gives me problems...

    $replace = __( apply_filters( 'em_event_output_placeholder', $replace ) );

    If i disable it, all works fine without errors/warnings... can you explain me why?

    These are the warnings gave from the above line of code...

    Warning: Missing argument 2 for em_event_output_placeholder() in
    .../wp-content/plugins/events-manager/classes/em-event.php on line 2144

    Warning: Missing argument 3 for em_event_output_placeholder() in
    .../wp-content/plugins/events-manager/classes/em-event.php on line 2144

    Warning: Missing argument 2 for my_custom_event_placeholders() in
    .../wp-content/plugins/my-em-custom/my-em-custom.php on line 17

    Warning: Missing argument 3 for my_custom_event_placeholders() in /web/htdocs/www.vitaecrescita.com/home/wp-content/plugins/my-em-custom/my-em-custom.php on line 17

  19. Daniele
    Member
    Posted 1 year ago #

    What would does it for this piece of code $replace = __( apply_filters( 'em_event_output_placeholder', $replace ) ); and why if i disable it, the code still works?

  20. Daedalon
    Member
    Posted 1 year ago #

    That line is disabled in my original code above. I don't remember why it's there in the first place - perhaps it's a leftover from an old version of EM, where we took the original piece of code in the first place and then started modifying until it did what we wanted. Perhaps it served some theoretical purpose, so we left it as disabled instead of removing it entirely, but the theoretical need has never become a practice.

    You can remove that line entirely or leave it commented.

    That's one of the troubles with having custom placeholders instead of ones supported by core: when something changes in core, it's up to you to reflect the change. Of course on the other hand this way there's less work for EM developers, so the plugin development proceeds faster.

  21. Daniele
    Member
    Posted 1 year ago #

    Thanks Deadlon for your support. It was been precoius! :)

  22. angelo
    NetWebLogic Support
    Posted 1 year ago #

    you can try something like

    add_filter('em_event_output_placeholder','my_em_placeholder_mod',1,3);
    function my_em_placeholder_mod($replace, $EM_Event, $result){
    	-------
    	return $replace;
    }
  23. Daedalon
    Member
    Posted 1 year ago #

    Glad to hear of your appreciation, Daniele :)

  24. giscist
    Member
    Posted 1 year ago #

    Hi,
    What I've been dreaming of!!!!
    I tried to modify my theme's functions.php with great hope, but it does not work : instead of a tidy date, I get "#_CUSTOMDATEPLACEHOLDERNAME" - or "#_CUSTOMEVENTDATES" according to what modification I did.
    This is what I added in my theme's functions.php around line 87 (I replaced 'M' by 'F' as in my settings - full month in French).
    Any idea why it goes wrong?
    Thank you

    // DATE TIDIER

    add_filter( 'em_event_output_placeholder', 'my_custom_event_placeholders', 1, 3);
    /**
    * Custom Events Manager placeholders
    * @param string $result
    * @param EM_Event $EM_Event
    * @param string $placeholder
    * @return string
    */
    function my_custom_event_placeholders( $replace, $EM_Event, $result ) {
    switch ( $result ) {
    case '#_CUSTOMEVENTDATES':
    $replace = '';
    $start_date = strtotime( $EM_Event->event_start_date );
    $end_date = strtotime( $EM_Event->event_end_date );
    $timediff = $end_date - $start_date;
    // Check if the number of the day is different
    if ( $timediff >= 86400 ) {
    // Check if is the same month
    if ( date_i18n( 'F', $end_date ) == date_i18n( 'F', $start_date ) ) {
    // Print only the start day
    $replace = date_i18n( 'j', $start_date ). ' - ' . date_i18n( 'j F Y', $end_date );
    }else{
    // Check if is the same year
    if ( date_i18n( 'Y', $end_date ) == date_i18n( 'Y', $start_date ) ) {
    // Print only the start day and month
    $replace = date_i18n( 'j F', $start_date ). ' - ' . date_i18n( 'j F Y', $end_date );
    }else{
    // Print the complete start and end date
    $replace = date_i18n( 'j F Y', $start_date ). ' - ' . date_i18n( 'j F Y', $end_date );
    }
    }
    }else{
    $replace = date_i18n( 'j F Y', $start_date );
    }
    break;
    }
    return $replace;
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Events Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.