Skip Navigation

[Resolved] convert non-types date field timestamp to date

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

Problem:

The issue here is that the user has a timestamp stored in a custom field.

Solution:

To do this you will need to add this custom shortcode to your functions.php file.

// Add Shortcode
function convert_date( $atts ) {
   
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_field' => '',
            'post_id'=> '',
        ),
        $atts
    );
    $field_value = get_post_meta($atts['post_id'],$atts['post_field']);
    $date = date('F j, Y, g:i a', $field_value[0]);
    return $date;
   
}
add_shortcode( 'convert_date', 'convert_date' );

Then you use this shortcode by doing the following.

[convert_date post_field='evcal_srow' post_id='12']

Now the shortcode takes 2 parameters, the name of the field and the id of the post.

This support ticket is created 6 years, 4 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)

This topic contains 10 replies, has 4 voices.

Last updated by KonstantinS88 6 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#921753

Hello

I create Events with the EventOn plugin, and I show them on a toolset map as markers (I'll probably show them later in other views too, but if solved that will be tha same case). I need to show the event date and time in the marker popup below the event title. You can take a look here how it shows now: hidden link

I tried this solution from the forum: https://toolset.com/forums/topic/converting-a-timestamp-to-date-and-time/ - the only modification I took was to convert the value of the timestamp to integer in the php code, because it gave me the error that the date function's second attribute must not be a string. Now it show this date everywhere: January 1, 1970, 12:00 am.

#921932

I think this question is better directed to the Plugin that created the Field, or consultants as listed here:
https://toolset.com/contractors/

This because the Field you use does not seem to be created with Toolset, as we offer ShortCodes to display the date fields, and you state the Field is from EventOn Plugin.

That plugin must provide some sort of method to display their Date.

However, if that date you receive is a Timestamp, all you need to do in order to convert it to a human readable format is use this PHP function:
hidden link
Example

$timestamp = 1299446702:
echo date('m/d/Y', $timestamp);//In a shortcode you return this

However, this is not part of our Support Umbrella:
https://toolset.com/toolset-support-policy/

If you created the Date Field with Types, you can display it with the ShortCode or functions:
https://toolset.com/documentation/customizing-sites-using-php/functions/

#922607

Hello Beda

I made some more tests and it seems on a simple view I can get the date easily.
Here is a test page for it: hidden link

The problem seems to be with the nested view your colleague Luo Yang helped me to set up. Here is the marker code from it:

[wpv-map-marker map_id="map-3" marker_id='marker-[wpv-post-id]-[wpv-attribute name="eid"]' marker_field="wpcf-lokacio" marker_title=" "][wpv-post-link id='[wpv-attribute name="eid"]']<br />Dátum: [convert_date timestamp="[wpv-post-field name='evcal_srow']"]<br />Hely: [wpv-post-link][/wpv-map-marker]

Maybe I should pass this

 id='[wpv-attribute name="eid"]'

to the

timestamp="[wpv-post-field name='evcal_srow']"]

. But too many quote characters I guess because it is not working. Can you help me to further change the code your colleague helped me with to show the date in the marker popup?

#922703

Shane
Supporter

Languages: English (English )

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

Hi Joseph,

The Idea on how to use the quotes is that once you use a Double Quote then within that you will always use single quotes.

Example


[shortcode param="[shortcode param ='[shortcode param ='test']']"]

Please let me know if this clears things up.
Thanks,
Shane

#922801

Hi Shane

Unfortunately the problem persists. The output is now: January 1, 1970, 12:00 am“]

I also listed the timestamps under the map tos ee if the output from the db is correct, and it is correct: hidden link

And the shortcode fuction you recommended in you reply to somebody's question also works, so the problem must be somehow in the marker syntax, especially when it outputs “] in the end of the wrong epoch date.

This is the code i have in the child view loop:

Helyek: [wpv-post-link]
 | Event: [wpv-post-link id='[wpv-attribute name="eid"]']
 | Date: [wpv-post-field name="evcal_srow" id="[wpv-attribute name='eid']"]
 | [types field='lokacio'][/types]
[wpv-map-marker map_id="map-3" marker_id='marker-[wpv-post-id]-[wpv-attribute name="eid"]' marker_field="wpcf-lokacio" marker_title=" "][wpv-post-link id='[wpv-attribute name="eid"]']<br />Dátum: [convert_date timestamp="[wpv-post-field name='evcal_srow' id='[wpv-attribute name='eid']']"]<br />Hely: [wpv-post-link][/wpv-map-marker]
#923047

Shane
Supporter

Languages: English (English )

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

Hi Jose,

This will always be a static field ? Or will it always call [wpv-attribute name='eid'] ?

If so then you should be able just hardcode this within the shortcode .

Thanks,
Shane

#923080

Well, actually I don't know. Luo Yang set me up this nested view in which I have a Parent View for Evens which loops this:

[wpv-view name="child-view-helyek-posts" eid="[wpv-post-id]"]

And a child view for the Helyek post type (it means Places in hungarian) which loops the markers data, you have it's code above.

Maybe you could have a look directly on my admin for this....?

Actually this whole nested view with this eid attribute is still a little confusing to me (maybe if you know about a tutorial, other than the help doc about this, that would be really helpful). I'll also will have to set up a filters for this with taxonomy categories, and it would be helpful to understand more.

#923625

Shane
Supporter

Languages: English (English )

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

Hello,

Yes please provide me with access so that I can have a look.

Thanks,
Shane

#924201

Shane
Supporter

Languages: English (English )

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

Hello,

So what I did was to allow the shortcode to take the ID as a separate parameter.

// Add Shortcode
function convert_date( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_field' => '',
			'post_id'=> '',
        ),
        $atts
    );
    $field_value = get_post_meta($atts['post_id'],$atts['post_field']);
    $date = date('F j, Y, g:i a', $field_value[0]);
    return $date;
  
}
add_shortcode( 'convert_date', 'convert_date' );
[convert_date post_field='evcal_srow' post_id='[wpv-attribute name="eid"]']

It should now be working fine.

Thanks,
Shane

#924205

Thanks a lot Shane, it works perfectly now!!

#1692035

It kind of works, but the date has the wrong language :O

Edit: Use date_i18n instead of date to make it mulitlanguage