Last active
August 13, 2022 00:53
-
-
Save buagern/96f7c1fe7847e0122fa2c215426ed058 to your computer and use it in GitHub Desktop.
PHP Thai locale date Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function dateth() { | |
| if (0 === $given = func_num_args()) { | |
| return trigger_error(__FUNCTION__.'() expects at least 1 parameter, '.$given.' given', E_USER_WARNING); | |
| } | |
| $mapping = [ 'D' => [ 'Mon' => 'จ.', 'Tue' => 'อ.', 'Wed' => 'พ.', 'Thu' => 'พฤ.', 'Fri' => 'ศ.', 'Sat' => 'ส.', 'Sun' => 'อา.' ], 'l' => [ 'Monday' => 'จันทร์', 'Tuesday' => 'อังคาร', 'Wednesday' => 'พุธ', 'Thursday' => 'พฤหัสบดี', 'Friday' => 'ศุกร์', 'Saturday' => 'เสาร์', 'Sunday' => 'อาทิตย์' ], 'M' => [ 'Jan' => 'ม.ค.', 'Feb' => 'ก.พ.', 'Mar' => 'มี.ค.', 'Apr' => 'เม.ย.', 'May' => 'พ.ค.', 'Jun' => 'มิ.ย.', 'Jul' => 'ก.ค.', 'Aug' => 'ส.ค.', 'Sep' => 'ก.ย.', 'Oct' => 'ต.ค.', 'Nov' => 'พ.ย.', 'Dec' => 'ธ.ค.' ], 'F' => [ 'January' => 'มกราคม', 'February' => 'กุมภาพันธ์', 'March' => 'มีนาคม', 'April' => 'เมษายน', 'May' => 'พฤษภาคม', 'June' => 'มิถุนายน', 'July' => 'กรกฎาคม', 'August' => 'สิงหาคม', 'September' => 'กันยายน', 'October' => 'ตุลาคม', 'November' => 'พฤศจิกายน', 'December' => 'ธันวาคม' ]]; | |
| $args = func_get_args(); | |
| $ts = ($args[1]) ?? time(); | |
| // var_dump(implode('', array_keys($mapping))); | |
| $mapregex = '/['.(implode('', array_keys($mapping))).']/'; | |
| $args[0] = preg_replace_callback_array([ | |
| '/[yYo]/' => function ($match) use($args, $ts) { | |
| $year = (int) date('Y', $ts); | |
| $year = $year + 543; | |
| if ($match[0] == 'y') { | |
| $year = substr($year, -2); | |
| } | |
| return $year; | |
| }, | |
| $mapregex => function ($match) use($args, $ts, $mapping) { | |
| return $mapping[$match[0]][date($match[0], $ts)]; | |
| } | |
| ], $args[0]); | |
| return call_user_func_array('date', $args); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment