Skip Navigation

[Resolved] can i convert a custom field’s date format?

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

Problem:

The issue here is that the user wanted to convert his date time from the database that was created using The Events Calendar plugin. The customer retrieved the value using [wpv-post-field name='_EventStartDateUTC']

Solution:

This was actually done by using a custom shortcode.

// Add Shortcode
function change_date( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'date' => '',
            'format' => '',
        ),
        $atts
    );
  
    $originalDate = $atts['date'];
    $newDate = date($atts['format'], strtotime($originalDate));
    return $newDate;
  
}
add_shortcode( 'change_date', 'change_date' );

The shortcode above takes a date in any format and converts it to the desired format.

Example

[change_date date="[wpv-post-field name='_EventStartDateUTC']" format='Y-m-d H:i:s"']
This support ticket is created 6 years, 5 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.

Our next available supporter will start replying to tickets in about 8.90 hours from now. Thank you for your understanding.

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)

This topic contains 8 replies, has 2 voices.

Last updated by Ido Angel 6 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#914775

hey,
i'm using "the Events Calendar", and the field [wpv-post-field name='_EventStartDateUTC'] to show the events start date.
but the format is y-m-d, and i want to change it to d-m-y.
is there any way (maybe jquery?) to make this possible?
thanks!
ido

#914871

Shane
Supporter

Languages: English (English )

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

Hi Ido,

Thank you for contacting our support forum.

I would suggest that you create a custom shortcode that can get the date from this shortcode or from the database and then format it in the way you want it.

Would you like for me to assist with writing the shortcode to do this ?

Thanks,
Shane

#914873

hi shane! yes that would be great!
the current format is:

y-m-d hr:min
(2018-12-22 06:00)

thanks!

#914912

Shane
Supporter

Languages: English (English )

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

Hi Ido,

Thank you for the patience,

Could you try the code below and let me know if it helps.

// Add Shortcode
function change_date( $atts ) {

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

	$originalDate = $arr['date'];
	$newDate = date($arr['format'], strtotime($originalDate));
	
	return $newDate;

}
add_shortcode( 'change_date', 'change_date' );

You can use this by adding it to your functions.php file and then pass the shortcode value into the shortcode like this.

[change_date date="[wpv-post-field name='_EventStartDateUTC']" format='Y-m-d H:i:s"']

Please try and let me know.

Thanks,
Shane

#914925

hey shane,

thanks - but that didn't work. now the field returns empty...

ido

#914930

Shane
Supporter

Languages: English (English )

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

Hi Ido,

Would you mind providing me with access to the site so that I can check on this for you ?

Also please let me know the page that you are testing this.

Thanks,
Shane

#914931

Shane
Supporter

Languages: English (English )

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

The private fields have been enabled

#914964

Shane
Supporter

Languages: English (English )

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

Hi Ido,

Thanks,

It was an error on my part by using the wrong variable name.

// Add Shortcode
function change_date( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'date' => '',
            'format' => '',
        ),
        $atts
    );
 
    $originalDate = $atts['date'];
    $newDate = date($atts['format'], strtotime($originalDate));
    return $newDate;
 
}
add_shortcode( 'change_date', 'change_date' );

The corrected code is above and should now work.

Thanks,
Shane

#914968

perfect!
thanks a lot shane!