-1

How to get a date in the format 5 - 7 Jan

if (get_field('event_from')) {
    echo $datefrom->format('d.m.Y').' - ';
  }

return 21.01.2020 - 22.01.2020

But I need a date in a different format for example 5 - 7 Jan Please help.

Nikita Kurilovic
  • 343
  • 2
  • 3
  • 11
  • 1
    Did you have checked available `date()` formats, together with provided examples there? https://www.php.net/manual/en/function.date.php – mitkosoft Jan 21 '20 at 14:14
  • Does this answer your question? [How do I get the current date and time in PHP?](https://stackoverflow.com/questions/470617/how-do-i-get-the-current-date-and-time-in-php) – mightyWOZ Jan 21 '20 at 16:10

1 Answers1

1

If the ACF-field is a datepicker, you could configure the return format in the field it self. Setting it to j M will give you the value "5 Jan"

'j' is numeric representation of day without a leading zero. 'M' is a short textual representation of a month, three letters.

ACF Datepicker-field

Check the PHP-manual for more information: https://www.php.net/manual/en/function.date.php

Once the field is configured to return the date formated as above, you can just append the value to your string.

$date_string;

if (get_field('event_from')) {
   $date_string = get_field('event_from') . ' - ';
}

if (get_field('event_to')) {
   $date_string .= get_field('event_to');
}

echo $date_string;
ninja
  • 2,233
  • 1
  • 15
  • 15