0

I've a custom field for a date, written like: yyyy-mm-dd

How can I extract only the day to display in PHP ? For the moment, I display the full date:'. get_post_meta($post->ID, 'event_start_date', true).'

Thank you :)

1 Answer 1

0

You can try the date() function:

$date = get_post_meta($post->ID, 'event_start_date', true);
$day = date('d', strtotime($date)); //01-31

Another way will be DateTime:

$date = get_post_meta($post->ID, 'event_start_date', true);
$date = DateTime::createFromFormat('Y-m-d', $date);
$day  = $date->format('d');
2
  • Thanks ! But it displays 01 value every time.
    – Garrosh
    Commented Nov 13, 2017 at 11:52
  • What do you mean? this should display the day (01-31) depending on the meta field event_start_date. So this for the same $post->ID will always display the same value. Commented Nov 13, 2017 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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