Skip Navigation

[Résolu] date picker convert to wrong year using php

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 support ticket is created Il y a 7 années et 1 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par Luo Yang Il y a 7 années et 1 mois.

Assisté par: Luo Yang.

Auteur
Publications
#578926
date error in php.JPG
correct year.JPG

I am trying to: get the year of a date using php is offset by multiple years from the date picker year

Link to a page where the issue can be seen:
php code output : lien caché
custom post with selected date for September 9th 2011 : lien caché

I expected to see: the birth year of 2011

Instead, I got: birth year: 1970

i have used similar code on another custom post to calculate user birth year, age, and other calculated values successfully.
when i modify the code to use custom post field of date i get this error for only some years.
when i select in the date picker November 1st 1969 the date year on my test page render correctly (1969)
when i select in the date picker September 9th 2011the date year on my test page render the wrong year (1970)

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>';

please advise,
thanks,

David

#578966

Dear David,

It is a custom PHP codes question, 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);
...

More help:
lien caché

#578983

Thanks Luo,

great, this solve the issue.

the only question i have is why the difference between user custom meta field of date vs. post custom field of date.
this is confusing as in the thread we have at https://toolset.com/forums/topic/get-birth-date-year-using-php-and-calculate-current-age the data extract was different.

Thanks,

David

#578987

The custom post field is different from custom user field:
custom post fields store in database table "wp_postmeta",
custom user field store in database table "wp_usermeta",
In this thread:
https://toolset.com/forums/topic/get-birth-date-year-using-php-and-calculate-current-age/
It is using Types function "types_render_usermeta", you can also use wordpress function get_user_meta() to get the same result:
https://codex.wordpress.org/Function_Reference/get_user_meta
If you are using wordpress function get_user_meta() or get_post_meta(), you will need to pre-pend "wpcf-" before the field slug, if you are using Types function, it is not needed.

#578988

Thanks Luo,
I get it.

David

#579752

You are welcome.