Also is there a way to make it work with multiple different pods, not just ‘rebate_applications’?
Thanks!
I’m going to sleep at the moment so sorry if this is too short, but the date time string can be parsed to a date integer with https://www.php.net/manual/en/function.strtotime.php then parsed back to a string with https://www.php.net/manual/en/function.date.php
You can get the post type from the post ID with https://developer.ww.wp.xz.cn/reference/functions/get_post_type/
@reedus33
<?php
add_shortcode(
'date_only',
function( $atts, $content, $shortcode_tag ) {
return return_date_only( get_the_ID() );
}
);
function return_date_only( $id ) {
$date_time = get_post_meta( $id, 'application_date', true );
return date(
'M j, Y',
strtotime( $date_time )
);
}
Great, small modification – can you make it so I pass the field as well – application_date,
such that {@ID, application_date, return_date_only}
I have some other date time fields I’d like to use the function for.
Thanks either way!
-
This reply was modified 3 years, 5 months ago by
Reed Sutton.
Are you trying to use this as a magic tag? If so, the format would be {@application_date,return_date_only_for_magic_tag}, where the first item is the field on the current post object, and the second is the function to be used, which takes the date as an argument. The ID would not be used in this case:
<?php
function return_date_only_for_magic_tag( $date_time ) {
return date(
'M j, Y',
strtotime( $date_time )
);
}
See https://docs.pods.io/displaying-pods/magic-tags/#Passing_values_through_callbacks_and_filters
Or, as a shortcode which takes the attribute field, e.g., [date_only field="application_date"]:
<?php
add_shortcode(
'date_only',
function( $atts, $content, $shortcode_tag ) {
return return_date_only( get_the_ID(), $atts['field'] );
}
);
function return_date_only( $id, $field ) {
return date(
'M j, Y',
strtotime( get_post_meta( $id, $field, true ) )
);
}
See https://developer.ww.wp.xz.cn/reference/functions/add_shortcode/
If you have any additional changes to your custom requirement, please try yourself first and come with your closest solution.
Oh perfect that’s even simpler. Yes I am using it as a magic tag.
One more ask – what if I want to add 90 days or 3 months to the date as well?
See https://developer.ww.wp.xz.cn/reference/functions/human_time_diff/
It takes two Unix timestamps so pass it the values from strtotime() and now().
Like if the application_date is April 1, 2023, I want to display July 1, 2023…
Read the examples in the linked strtotime() documentation. It supports relative date calculations.
Thanks – worked. Code below for reference.
/*
Returns the date in 3 months within a magic tag.
Usage: {@field, return_date_three_months}
https://ww.wp.xz.cn/support/topic/custom-function-to-return-date-without-time/#post-16316867
*/
function return_date_three_months( $date_time ) {
return date(
'M j, Y',
strtotime( "+3 months", strtotime($date_time) )
);
}
Nice work! Thank you for sharing your result.