1

I'm using the PODS WordPress Framework which is amazing.

I created a template with one of the fields publishing the date of an event which is done through a simple 'magic tag' like this

{@date_of_event}

This publishes the date in this format: September, 15th 2018

What I need to do is change the format of the date being published into a numeric version which I can do by passing the value into my own function.

That would be done like this:

{@start_date,MY_FUNCTION}

So, to do this I need to create a function in my functions.php file like this:

function MY_FUNCTION ($input) {
       return "New Input" . $input;
}

Now, the php time date format that I need is like this:

Y/m/d

So - my question - how do I insert this into the function above?

Thanks for all help and direction on this!

2 Answers 2

3

You can use the PHP's strtotime() and WordPress's date_i18n() functions:

function MY_FUNCTION( $input ) {
    // Removes commas so that we'll get the proper timestamp. Otherwise, the
    // strtotime() function would return FALSE.
    $input = str_replace( ',', '', $input );

    // Retrieve the UNIX timestamp of the date.
    $timestamp = @strtotime( $input );

    // Returns the date in *localized* format.
    return date_i18n( 'Y/m/d', $timestamp );
}

MY_FUNCTION( 'September, 15th 2018' ), for example, would give you 2018/09/15.

10
  • Thanks for that - really appreciate your help. It didn't work for me thou - is it not missing this line at the end: add_filter( 'MY_FUNCTION' );
    – Henry
    Sep 7, 2018 at 5:32
  • Did you add the code to your theme functions file? (i.e. functions.php) By using the 'magic tag' {@start_date,MY_FUNCTION}, the function should be called automatically by the PODS framework plugin. See pods.io/docs/build/using-magic-tags
    – Sally CJ
    Sep 7, 2018 at 5:39
  • Yes I did add to the functions.php file. Does it matter that the comma is actually AFTER the date? So the comma actually is placed in the above example exactly like this: 15th,
    – Henry
    Sep 7, 2018 at 5:46
  • I don't really understand that, but it is necessary for the function to remove the comma there. Otherwise, we wouldn't get the correct timestamp - e.g. 1536969600 (correct) versus false (incorrect..). Try add echo $timestamp; after the $timestamp = ... in the function, and see if the value is good.
    – Sally CJ
    Sep 7, 2018 at 5:50
  • 1
    CJ - you were right. Sorry - I was looking at the wrong template. THANK You VERY much for all your help - works great.
    – Henry
    Sep 7, 2018 at 6:15
0

If you don't need the date in a different format somewhere else: it's far easier go into pods admin -> edit -> choose your pod / field

Go into Advance Field Options fill in your Choice into "Date format for display"

2
  • I can't because I need the date published from two different formats in same template
    – Henry
    Sep 7, 2018 at 6:04
  • This should work then: function MY_FUNCTION($input_date) { return date("Y/m/d", strtotime($input_date)); }
    – Quasel
    Sep 7, 2018 at 6:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.