Skip Navigation

[Resolved] format the “event calendar” date outputted by views

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here was that the user was using the event calendar plugin to display their date fields, however when retrieving the date values with the [wpv-post-field name="_EventStartDate"] shortcode that our views plugin provides they are getting a timestamp value.

Solution:

To resolve this you will need to use some custom code to convert the timestamp to a human readable format.

Add the following to your toolset custom code section in Toolset->Settings-> Custom code and activate it.


// Add Shortcode
function format_date( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'date' => '',
        ),
        $atts
    );
 
    $date = $atts['date'];
     
    return date("l M, d, Y", strtotime($date));
 
}
add_shortcode( 'format_date', 'format_date' );

This shortcode will convert the timestamp into a human readable date.

An example usage is [format_date date="[wpv-post-field name='_EventStartDate']"]

This support ticket is created 5 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 12 replies, has 2 voices.

Last updated by yalcinA 5 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1341409
Screen Shot 2019-09-16 at 19.32.37.png

Tell us what you are trying to do?
Make the date output look more user friendly. ie: "30 September 2019" - rather than "2019-09-30 18:30:00"
Is there any documentation that you are following?
no
Is there a similar example that we can see?

What is the link to your site?
hidden link

#1341455

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Yalcin,

Thank you for getting in touch.

Could you let me know the code you are using to display the date?

Thanks,
Shane

#1341465

Hello Shane. I am using a shortcode to view a custom post type created by the "event calendar" plugin:

[wpv-post-field name="_EventStartDate"]

#1341479

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Yalcin,

Ok so the problem is with how events calendar is storing the date fields.

You will need another custom code to change the format of the date.

Add the following shortcode to your custom toolset code and activate it.

// Add Shortcode
function format_date( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'date' => '',
		),
		$atts
	);

	$date = $atts['date'];
	
	return date("l M, d, Y", strtotime($date));

}
add_shortcode( 'format_date', 'format_date' );

This can be added to Toolset -> Settings -> Custom Code

[format_date date="[wpv-post-field name='_EventStartDate']"]

Please try this and let me know if this helps.
Thanks,
Shane

#1341487

I added the custom code but no difference...

Do I also need to add this line somewhere else: [format_date date="[wpv-post-field name='_EventStartDate']"]

?

#1341491

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Yalcin,

Would you mind allowing me to have admin access to the site as well as a link to the page where I can test this ?

The private fields have been enabled for your next response.

Thanks,
Shane

#1341507

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi yalcin,

I tested this on your site and it works perfectly fine.
hidden link

Thanks,
Shane

#1341667

Shane, that page was already fine before you did anything: That output is Event Calendar's own native output and it can be set from its own settings page. The issue starts when toolset views takes data from Event Calendar's database and displays in custom views.

See here: hidden link

#1341669

Correction: Looking into what you've done, now I see that you've used that different code in the view formulation. That means somethings have changed. I will try to replicate that in other locations as well.

#1342131

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Yalcin,

Not sure what you meant by this here " now I see that you've used that different code in the view formulation. That means somethings have changed" as in your view i used the exact code i provided here [format_date date="[wpv-post-field name='_EventStartDate']"]

Could you let me know if everything is ok now?

Thanks,
Shane

#1342145

Shane, all is good now. Thank you very much. One last question: can you point me to a documentation that has more guidence on toolset date formats?

#1342169

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Yalcin,

The date formats only work with our Toolset Types shortcode that is retrieving the data from our Toolset Field.

Here is the documentation on the date fields.
https://toolset.com/documentation/customizing-sites-using-php/functions/#date

Also here is the documentation on how to format the data.
https://wordpress.org/support/article/formatting-date-and-time/

Please let me know if this helps.
Thanks,
Shane

#1342527

My issue is resolved now. Thank you!