'ม.ค.', 2 => 'ก.พ.', 3 => 'มี.ค.', 4 => 'เม.ย.', 5 => 'พ.ค.', 6 => 'มิ.ย.', 7 => 'ก.ค.', 8 => 'ส.ค.', 9 => 'ก.ย.', 10 => 'ต.ค.', 11 => 'พ.ย.', 12 => 'ธ.ค.' ); $monthsThaiLong = array( 1 => 'มกราคม', 2 => 'กุมภาพันธ์', 3 => 'มีนาคม', 4 => 'เมษายน', 5 => 'พฤษภาคม', 6 => 'มิถุนายน', 7 => 'กรกฎาคม', 8 => 'สิงหาคม', 9 => 'กันยายน', 10 => 'ตุลาคม', 11 => 'พฤศจิกายน', 12 => 'ธันวาคม' ); try { $dateObj = new DateTime($date); $day = $dateObj->format('d'); $month = $dateObj->format('n'); $year = (int)$dateObj->format('Y') + 543; $day = (int)$day; if ($format === 'long') { return $day . ' ' . $monthsThaiLong[$month] . ' ' . $year; } else { return $day . ' ' . $monthsThai[$month] . ' ' . $year; } } catch (Exception $e) { return $date; } } } // Custom PDO Statement class ที่ auto-format วันที่ if (!class_exists('PDOStatementFormatDate')) { class PDOStatementFormatDate extends PDOStatement { public function fetch($fetch_style = null, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) { $args = func_get_args(); $result = call_user_func_array(array('PDOStatement', 'fetch'), $args); if (is_array($result)) { $result = $this->formatDates($result); } return $result; } public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = array()) { $args = func_get_args(); $result = call_user_func_array(array('PDOStatement', 'fetchAll'), $args); if (is_array($result)) { foreach ($result as &$row) { if (is_array($row)) { $row = $this->formatDates($row); } } } return $result; } private function formatDates($row) { foreach ($row as $key => &$value) { if (is_string($value) && isDateField($value)) { $value = formatThaiDate($value); } } return $row; } } } // Custom PDO class ที่ใช้ PDOStatementFormatDate if (!class_exists('PDOFormatDate')) { class PDOFormatDate extends PDO { public function prepare($statement, $options = array()) { $options[PDO::ATTR_STATEMENT_CLASS] = array('PDOStatementFormatDate'); return parent::prepare($statement, $options); } } } // กำหนดค่าการเชื่อมต่อฐานข้อมูล php version 5.3 $host = 'localhost'; $db = 'coachingDB'; $user = 'root'; $pass = 'EoD@drFU90'; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; try { $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset", PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); // ใช้ custom PDO class ที่ auto-format วันที่ $pdo = new PDOFormatDate($dsn, $user, $pass, $options); } catch (PDOException $e) { die("DB Error: " . $e->getMessage()); } ?>