date - Using property in PHP function
Get the solution ↓↓↓I am looping trough a JSON file with
foreach ($json->response as $value) {
echo date_format($value->arrival->scheduled_time, "H:i:s");
But thedate_format
PHP function doesn't work with$value->arrival->scheduled_time
. What am I missing?$value->arrival->scheduled_time
contains a date. For example2020-12-27T19:55:00.000
Answer
Solution:
expects parameter one to be a
. You are giving it a string. Assuming that string is in a valid datetime format, you need to ctreate a
DateTime
object first and then calldate_format()
.
foreach ($json->response as $value) {
$datetime = date_create($value->arrival->scheduled_time);
echo date_format($datetime , "H:i:s");
or in OOP:
foreach ($json->response as $value) {
$datetime = new DateTime($value->arrival->scheduled_time);
echo $datetime->format("H:i:s");
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: malformed utf-8 characters, possibly incorrectly encoded
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.