1

I have a date field I'm pulling from WordPress, where the default format is:

16/12/2016

Been a while since I played with PHP, but I remember the way to format dates was: echo date_format($date,"[format syntax]");

But when I do

$date = myWPField;
echo date_format($date,"F d, y");

If doesn't display the date.

What am I missing. Has this changed?

1
  • is there any error? is the myWPField DateTime object? Commented Dec 22, 2016 at 1:59

2 Answers 2

2

Since your date is not an acceptable format in date_create like mm/dd/yy(yy) or mm-dd-yy(yy)

Use php DateTime::createFromFormat function instead this function can detect what format you

(PHP 5 >= 5.3.0, PHP 7) DateTime::createFromFormat -- date_create_from_format — Parses a time string according to a specified format

$date = '16/12/2016';
$show_date = DateTime::createFromFormat('d/m/Y', $date)->format('F d, y');

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

when create data format form string use slash format, it's in the mm/dd/yy(yy) format. So in you code your first 16 is not allowed by default, you have to specify in what format to create from the string.

Note that when you create a new date object using a format with slashes and dashes (eg 02-02-2012 or 02/02/2012) it must be in the mm/dd/yy(yy) or mm-dd-yy(yy) format (rather than british format dd/mm/yy)! Months always before years (the american style) otherwise you'll get an incorrect date and may get an error like the one above (where PHP is crashing on trying to decode a 13th month).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.