แสดงบทความที่มีป้ายกำกับ ภาษาไทย แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ ภาษาไทย แสดงบทความทั้งหมด

วันเสาร์ที่ 8 มีนาคม พ.ศ. 2557

แปลงวันที่ไทยให้เป็นวันที่ของ php

แปลงวันที่ภาษาไทยให้เป็นวันที่ php ครับ แต่ function นี้มีข้อจำกัดคือไม่สามารถใช้กับ host windows ได้ครับเพราะเรียกใช้ strptime ซึ่งไม่มีใน host windows
function strtotime_thai($str){
    $str  = preg_replace_callback('/\d{4}/',function($match){
         return $match[0]-543;
    },$str);
    setlocale(LC_TIME, 'thai');
    $format = '%d %B %Y %H:%M:%S';//d M Y H:i:s
    $str = iconv('UTF-8','TIS-620',$str);//comment this line if $str is tis620
    $result = strptime($str,$format);
    if (empty($result)){
        return false;
    }
    extract($result);
    return mktime($tm_hour,$tm_min,$tm_sec,$tm_mon+1,$tm_mday,$tm_year+1900);
}
$phptime = strtotime_thai('19 ธันวาคม 2557 23:30:59');
echo date('d/m/Y H/i/s',$phptime);

วันเสาร์ที่ 26 ตุลาคม พ.ศ. 2556

การเรียงลำดับภาษาไทยใน php / sorting thai characters


ในการเรียงลำดับโดย php ถ้าใช้ฟังค์ชั่น sort ธรรมดา จะเรียงทีละ byte หรือเรียงโดยให้ความสำคัญกับสระเท่าพยัญชนะจะทำให้เกิดการเรียงลำดับภาษาไทยที่ผิดพลาดดังนนี้ครับ

การบ้าน,ข้าว,เคย,โค,ไก่,ไข่

(ทำให้คำที่ขึ้นต้นด้วยสระเดียวกันเช่น ไ (สระไอไม้มาลัย) อยู่ติดกัน)
ใน php เราจะใช้การตั้งค่า locale และใช้ usort (User sort) ช่วยให้จัดเรียงลำดับภาษาไทยได้ถูกต้อง ดังนี้ครับ..

function sortthaitis620(&$a){
    setlocale(LC_COLLATE, 'thai');
    usort ($a, 'strcoll');
}
function sortthai(&$a){//สำหรับ utf8
    foreach($a as &$i){
      $i = iconv('UTF-8','TIS-620',$i);
    }
    setlocale(LC_COLLATE, 'thai');
    usort ($a, 'strcoll');
    foreach($a as &$i){
      $i = iconv('TIS-620','UTF-8',$i);
    }
}
$a = array('โค','เคย','ไก่','การบ้าน','ข้าว','ไข่');
sortthai($a);
echo implode(',',$a);

sortthaitis620 เป็น function สำหรับ เรียงลำดับข้อความภาษาไทยในตัวอักษรที่เข้ารหัสแบบ tis620 หรือ windows874

sortthai เป็น function สำหรับ เรียงลำดับข้อความภาษาไทยในตัวอักษรที่เข้ารหัสแบบ utf8 ครับ

แต่ถ้าต้องการเรียงลำดับข้อมูลในตารางของฐานข้อมูล mysql ที่ encoding เป็น utf8 ให้ select ดังนี้ครับ
SELECT * FROM tb ORDER BY CONVERT (column_name USING tis620);

วันพฤหัสบดีที่ 21 ตุลาคม พ.ศ. 2553

num2thai by dr yes


<?php //number to thai function by Dr.Yes<br>
function num2thai($thb) {
list($thb, $ths) = explode('.', $thb);
$ths = substr($ths.'00', 0, 2);
$thaiNum = array('','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า');
$unitBaht = array('บาท','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน');
$unitSatang = array('สตางค์','สิบ');
$THB = '';
$j = 0;
for($i=strlen($thb)-1; $i>=0; $i--,$j++) {
$num = $thb[$i];
$tnum = $thaiNum[$num];
$unit = $unitBaht[$j];
switch(true) {
case $j==0 && $num==1 && strlen($thb)>1: $tnum = 'เอ็ด'; break;
case $j==1 && $num==1: $tnum = ''; break;
case $j==1 && $num==2: $tnum = 'ยี่'; break;
case $j==6 && $num==1 && strlen($thb)>7: $tnum = 'เอ็ด'; break;
case $j==7 && $num==1: $tnum = ''; break;
case $j==7 && $num==2: $tnum = 'ยี่'; break;
case $j!=0 && $j!=6 && $num==0: $unit = ''; break;
}
$S = $tnum . $unit;
$THB = $S . $THB;
}
if($ths=='00') {
$THS = 'ถ้วน';
} else {
$j=0;
$THS = '';
for($i=strlen($ths)-1; $i>=0; $i--,$j++) {
$num = $ths[$i];
$tnum = $thaiNum[$num];
$unit = $unitSatang[$j];
switch(true) {
case $j==0 && $num==1 && strlen($ths)>1: $tnum = 'เอ็ด'; break;
case $j==1 && $num==1: $tnum = ''; break;
case $j==1 && $num==2: $tnum = 'ยี่'; break;
case $j!=0 && $j!=6 && $num==0: $unit = ''; break;
}
$S = $tnum . $unit;
$THS = $S . $THS;
}
}
return $THB.$THS;
}
$thb = mt_rand()/100; //random number;
echo number_format($thb,2)."<br>";
echo num2thai($thb);
?>