Here is my code:
$original_date = get_field('event_date');
$date = DateTime::createFromFormat('lFdY', strtotime($original_date));
$new_date = $date->format('l, F d, Y');
This code is in a custom post loop in a shortcode and called in the WordPress admin.
I have also tried it without the strtotime
. I am using PHP 5.6.
The event_date
field is from an Advance Custom Fields date picker.
If I just pass the field, I get my intended output (the input from the custom post admin page), but without whitespace and commas. I also set this, save and display values in the acf in WP.
This is the error:
Fatal error: Call to a member function format() on boolean on the line
with $new_date variable.
Then if I use:
$date = date("l, F d, Y", strtotime($original_date));
The loop throws an error, undefined variable, for each instance of the post.
You should pass the string directly to the
createFromFormat
method:The
boolean
you’re receiving is becausecreateFromFormat
is failing and returningfalse
.If you’re still receiving an error,
get_field()
is likely not returning what you think it is.Try using this ..
<?php echo date('l, F d, Y', strtotime(get_field('event_date')));?>
it is working at my endDateTime::createFromFormat returns
false
if it fails to convert the input string using the format specified.In your case, you’re taking an
$original_date
, converting it to a numeric timestamp, and trying to pass that in toDateTime::createFromFormat
, which isn’t going to work, hence the error. You’ll need to pass in the original date instead:PHP currently can not parse that specific format string. You can demonstrate this with an easy case that should “obviously” work. This:
returns
false
. The issue is specifically because of the lack of space betweenl
andF
in the format string and between the day-of-week and month in your input string.You should use a different date format. If you can remove the text representation of the day-of-week (because it’s not actually necessary to disambiguate the date), or at least get a space between the day-of-week and the month, that would be best.
Since you have existing data in your database in an unhelpful format, you could write a relatively simple pre-processor to remove the day-of-week from the input string:
(This example assumes always english day names, in lowercase, and the input is always valid. You may need more complicated code for your actual data.)
Nitty, gritty details: When PHP processes the
l
formatter (day of week), it requires seeing one of the characters in:,;:/.-()
, a space, tab, or end-of-string as an indication of where to stop reading.So, if your date string is
FridayJuly082016
, because there are no spaces whatsoever in the input date, the code continues reading the entire string, and then fails because (literally)FridayJuly082016
is not in the list of days of the week.Unfortunately for your particular case, this is also not likely to be “fixed”. You have an extremely unusual date format, and making this particular case work in the code would make it considerably more complicated, for a questionable and rare use case.
@jbafford I found a solution.
First I save the date format to yymmdd in the ACF datepicker field
Then I used this code to extract the year month and date separately and then formatted the strtotime result:
What confused me I think and got me hung up was the presumption that I had to pass l or the day into the strtotime and that I had to match input with the intended formatted output. PHP date doesn’t need that much info to accomplish the task. Thanks for all of your input!