Problem:
Get the year value of a custom date field using PHP code.
the code i'm using in my test page is:
$post_ID = 1666; $cld_bd = get_post_meta($post_ID, 'wpcf-child-birth-date', true); $cld_str_schl_age = get_post_meta($post_ID, 'wpcf-start-school-at-age', true); $cld_age = floor((time() - $cld_bd)/(365*24*60*60)); $cld_birth_year = date (Y, floor ($cld_bd/(365*24*60*60))); echo '<p>The BD year for the child is ' . $cld_birth_year . ' age: ' . $cld_age . ' and the start school age is' . $cld_str_schl_age .'</p>';
Solution:
I assume the custom field "child-birth-date" is a custom date field created with Types plugin, Types stores time-stamp value into custom date field, so you can get the year from time-stamp value like this:
... $cld_bd = get_post_meta($post_ID, 'wpcf-child-birth-date', true); $cld_birth_year = date ('Y', $cld_bd); ...
Relevant Documentation:
http://php.net/manual/en/function.date.php
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 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Hong_Kong (GMT+08:00)
This topic contains 5 replies, has 2 voices.
Last updated by 7 years, 1 month ago.
Assisted by: Luo Yang.