0

I need help to convert JSON date into PHP date format I am trying to find a way to convert that date to m-d-Y format in php.

in my return I get

{
    "result":"ok",
    "ret":
          {"count":7,
           "setFirstResult":0,
           "setMaxResults":5,
           "data":[
                    {"id":218,
                     "name":"AAA",
                     "mes":"",
                     "updatetime":
                              {"date":"2019-09-18 12:25:42.000000",
                               "timezone_type":3,
                               "timezone":"Asia\/Taipei"
                              }
                     }

This is my controller

public function showMeg(Request $request, $page)
{
    $entityManager = $this->getDoctrine()->getEntityManager();

$per = 5;
$start = ($page-1)*$per;
$query = $entityManager->createQueryBuilder()
    ->select("r")
    ->from("MegBundle:message", "r")
    ->setFirstResult($start)
    ->setMaxResults($per);
$data = $query->getQuery()->getArrayResult();
$paginator = new Paginator($query, $fetchJoinCollection = false);
$count = count($paginator);
$pages = ceil($count/$per);  
return new JsonResponse(['result' => 'ok', 'ret' => ['data' => $data]]);
}

how can I convert normal time in that format using php. e.g date('Y-m-d H:i:s')

Lai
  • 15
  • 1
  • 8
  • Check this out: https://stackoverflow.com/questions/40282868/change-output-of-datetime-in-json-encode Perhaps your issue is similar? – Thrallix Sep 19 '19 at 09:42

1 Answers1

0

You can convert it like this:

$timezone = "Asia/Taipei";
$dateTime = "2019-09-18 12:25:42.000000";
try {
    $timezone = new \DateTimeZone($timezone);
    $date     = new \DateTime($dateTime, $timezone);
    echo $date->format('m-d-Y');
} catch (Exception $e) {
    echo 'Error converting date'
}
Claudio
  • 5,078
  • 1
  • 22
  • 33