ฟังก์ชัน PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php $thai_day_arr = array ( "อาทิตย์" , "จันทร์" , "อังคาร" , "พุธ" , "พฤหัสบดี" , "ศุกร์" , "เสาร์" ); $thai_month_arr = array ( "0" => "" , "1" => "มกราคม" , "2" => "กุมภาพันธ์" , "3" => "มีนาคม" , "4" => "เมษายน" , "5" => "พฤษภาคม" , "6" => "มิถุนายน" , "7" => "กรกฎาคม" , "8" => "สิงหาคม" , "9" => "กันยายน" , "10" => "ตุลาคม" , "11" => "พฤศจิกายน" , "12" => "ธันวาคม" ); function thai_date( $time ){ global $thai_day_arr , $thai_month_arr ; $thai_date_return = "วัน" . $thai_day_arr [ date ( "w" , $time )]; $thai_date_return .= "ที่ " . date ( "j" , $time ); $thai_date_return .= " เดือน" . $thai_month_arr [ date ( "n" , $time )]; $thai_date_return .= " พ.ศ." .( date ( "Yํ" , $time )+543); $thai_date_return .= " " . date ( "H:i" , $time ). " น." ; return $thai_date_return ; } ?> |
การใช้งาน
1 2 3 4 5 | // ถ้ามีการเก็บวันที่และเวลาในรูปแบบ timestamp // สามารถนำมาใช้งานในฟังก์ชันได้ดังนี้ // $eng=1224562026; กรณีเป็น timestamp $eng_date =time(); // แสดงวันที่ปัจจุบัน echo thai_date( $eng_date ); |
การประยุกต์ใช้งาน
1 2 3 4 5 6 | // ถ้ามีการเก็บวันที่และเวลาในรูปแบบ date // คือ 2008-10-31 ต้องเปลี่ยนเป็น timestamp // ด้วยคำส่ง strtotime("2008-10-31"); // แสดงวันที่ 31 เดือน ตุลาคม ปี 2008 แบบภาษาไทย $eng_date = strtotime ( "2008-10-31" ); echo thai_date( $eng_date ); |
ตัวอย่างผลลัพธ์
วันอังคารที่ 21 เดือนตุลาคม พ.ศ.2551 11:07 น.