0

I am using JDF library for converting databases dates in my panel. My programming language is php and i am using while loop for show data. database date type is datetime.For example : 2018-07-21 11:14:19.678896

<?php
Include_once "jdf.php";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $array = explode(' ', $row['date']);
    list($year, $month, $day) = explode('-', $array[0]);
    list($hour, $minute, $second) = explode(':', $array[1]);
    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
    date_default_timezone_set("Asia/Tehran");
    $converted_date = jdate("Y/m/d H:i:s", $timestamp);
    echo $row['full_name'] . ", reg date:" . $converted_date;
}

When i run code, Y/m/d become convert BUT for H:i:s just first row become convert. I really dont know why ...
Can you help me ?

O. Hebaby
  • 53
  • 1
  • 7

1 Answers1

0

It's probably simpler to use the inbuilt DateTime class to process your data:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $date = date_create_from_format('Y-m-d H:i:s.u', $row['date'], new DateTimeZone('UTC'));
    $date->setTimeZone(new DateTimeZone('Asia/Tehran'));
    $converted_date = jdate('Y/m/d H:i:s', (int)$date->format('U'));
    echo $row['full_name'] . ", reg date:" . $converted_date;
}

Output:

2018/07/21 15:44:19

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95