wordpress - What's the right format to create a PHP DateTime instance of '2016.04.30 PM 7:30' via DateTime::createFromFormat? -
i'm working on wordpress project should custom post metadata, convert datetime
instance, , math it.
when echo
get_post_meta, looks follows.
2016.04.30 pm 7:30
the format i'm using datetime
instance follows.
y.m.d g:i
but return value of datetime::createfromformat
false
.
// 2016.04.30 pm 7:30 $start_at = datetime::createfromformat( 'y.m.d g:i', get_post_meta(get_the_id(), 'as_date', true)); if ($start_at === false) { echo 'false format: ' . get_post_meta(get_the_id(), 'as_date', true); } else { echo $start_at->gettimestamp(); }
the result false format: 2016.04.30 pm 7:30
.
what missing here? think must trivial can't through.
testing, found problem character in format 'a'. poked around , found bug in php (that apparently not bug @ all!)
going through source code, looks not parse , pm until after hour has been parsed.
probably best bet quick pass through regular expression move am/pm end:
$thedate = get_post_meta(get_the_id(), 'as_date', true); $thedate = preg_replace("/([0-9.]+) ([ap]m) ([0-9:]+)/i", "$1 $3 $2", $thedate); $start_at = datetime::createfromformat('y.m.d g:i a', $thedate);
Comments
Post a Comment