Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hy, i want to count years of an actor.

The date that actor is born is stored in a custom field.

$date = get_field('data_nasterii');

Date stored in custom field is : yymmdd

How can i stick bouth togeder to make it work.

<?php
                 //date in mm/dd/yyyy format; or it can be in other formats as well
                 $birthDate = "12/17/1967";
                 //explode the date to get month, day and year
                 $birthDate = explode("/", $birthDate);
                 //get age from date or birthdate
                 $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
                 echo $age ."ani";
            ?>

I try this but this returned me year 2012

<?php
         //date in mm/dd/yyyy format; or it can be in other formats as well
         $birthDate = get_field('data_nasterii');
         //explode the date to get month, day and year
         $birthDate = explode("/", $birthDate);
         //get age from date or birthdate
         $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));
         echo $age ."ani";
    ?>
share|improve this question
    
The place to do pure PHP questions is Stack Overflow. –  brasofilo Feb 11 '13 at 22:22
    
@brasofilo I just had to fiddle with a crappy misfit feed last week and get wp running with it. See my answer: Lot's of WP involved ;) –  kaiser Feb 11 '13 at 22:23
add comment

2 Answers

up vote 2 down vote accepted

Assuming that your date format - for whatever reason - really is yymmdd instead of being set to what you defined in the admin settings section:

There's the native PHP function date_parse_from_format() that you can use in conjunction with the core setting. Just add the following plugin and use the custom Template Tag in your templates.

<?php
defined( 'ABSPATH' ) OR exit;
/**
 * Plugin Name: Real Date Template Tag
 */

function wpse85476_to_real_date( $date, $format )
{
    static $date_format;
    empty( $date_format ) AND $date_format = get_option( 'date_format' );

    $date_parts = date_parse_from_format(
         $format
        ,$date
    );

    // Error handling
    if ( 0 < $date_parts['error_count'] )
    {
        return current_user_can( 'manage_options' )
            ? new WP_Error(
                 'date'
                ,'Wrong date format. Adjust Date Parser.'
                ,$date
            )
            : $date;
    }

    return array(
         'date_parts' => $date_parts
        ,'real_date'  => date_i18n(
             $date_format
            ,mktime( 0, 0, 0, $date_parts['month'], $date_parts['day'], $date_parts['year'] )
        )
    );
}

Here's how you'd retrieve a properly formatted date.

$date = wpse85476_to_real_date( get_field( 'data_nasterii' ), 'yymmdd' );
$real_date = $date['real_date'];

And here's how you would retrieve the parts for calculation:

$date_parts = $date['date_parts'];
share|improve this answer
    
kaiser, can you tell mee how can achive this ... wordpress.stackexchange.com/questions/85444/… –  Batman Feb 11 '13 at 22:27
1  
Uou, thiz' black magic! –  brasofilo Feb 11 '13 at 22:30
add comment

Thanks for answer, i resolve the problem with this :

<?php
         //date in mm/dd/yyyy format; or it can be in other formats as well
         $data = get_field('data_nasterii');
         $date = date("d/m/Y", strtotime($data));
         //explode the date to get month, day and year
         $date = explode("/", $date);
         //get age from date or birthdate
         $age = (date("md", date("U", mktime(0, 0, 0, $date[0], $date[1],$date[2]))) > date("md") ? ((date("Y")-$date[2])-1):(date("Y")-$date[2]));
         echo $age ." ani";
?>

Code based on this answer on StackOverflow by "Sudhir"

share|improve this answer
1  
You copy/pasted the answer from this SO answer. If you do so, then be kind enough to state it (you even left the original comments in), as it else is a violation against the license. FYI: CC-BY-SA. –  kaiser Feb 11 '13 at 22:28
    
i do not copy, i try make it work that code with my custom field, i put in first time in top , but it retrive me year 2012. I try it for several time and finaly it worked it returned me the result i wanted. That is. –  Batman Feb 11 '13 at 22:32
    
You started your answer with the code from SO - you still have the exact same comments and half of your code lines is the exact same thing. What I'm trying to tell you is that you can't use code from SO without mentioning the author and the source, as this is simply the SO/SE license that is set for all questions and answers - see the bottom left "cc-Wiki"-Badge on every page. –  kaiser Feb 12 '13 at 7:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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