Skip Navigation

[Resolved] Custom date

This support ticket is created 6 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 3 voices.

Last updated by Anonymous 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#571318

Anonymous

Hi, I have a page in which I need to put a custom field that I don't know how to output.

In a page there is a text that says: "today, 23 years later 1994, etc etc etc"

So, what I want to do is not writing "23" an each year update the page, but to put a custom field like "1994-today", soit always output the difference from 1994 and today year.

#571462

Toolset allows you to format the value of a custom date field using any Date and Time format provided by WordPress, but what you're describing is a calculation based on a fixed past date and "today". This will require custom code. You can use PHP's DateTime::diff function to calculate the difference between two DateTime objects, then format the result:
hidden link

$datetime1 = new DateTime('1994-01-01');
$datetime2 = new DateTime('');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%y years since Jan 1, 1994.');
#571466

Anonymous

Not what I would like

#571477

Hi Simone,

I'm Mohammed, the Toolset support team leader.

I see that your request is not a complicated one and we can offer a solution for you.

I see that Christian's solution is a good one according to the description you've provided.
If you want a better solution, it would be nice if you guided us to the solution you like.

If you don't mind, we will be happy to implement a solution for your request.

Thanks.

#571484

Anonymous

I would like to use a Toolset field instead of php.
I mean something like that

[wpv-datetime="01/01/1994 - today" format="m"]
#571557

Types doesn't perform calculation between dates like this, but I can offer a custom shortcode:

add_shortcode( 'format_date_difference', 'format_date_difference_func');
function format_date_difference_func($atts = [])
{
  $atts = shortcode_atts([
    'start' => '',
    'end' => '',
    'format' => '%y years, %m months, %d days',
  ], $atts);
  $datetime1 = new DateTime($atts['start']);
  $datetime2 = new DateTime($atts['end']);
  $interval = $datetime1->diff($datetime2);
  return $interval->format($atts['format']);
}

Specify start, end and format in the shortcode:

[format_date_difference start="1994-01-01" end="" format="%y years"]  // 23 years
[format_date_difference start="1994-01-01" end="2010-05-31" format="%y years"]  // 16 years
[format_date_difference start="1994-01-01"]  // 23  years, 8 months, 18 days
[format_date_difference] // 0 years, 0 months, 0 days

These are the defaults, but you can change them in the $atts array above:
Default start: now
Default end: now
Default format: %y years, %m months, %d days

#572815

Anonymous

Ok

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.