  | 
              
	              
	                
  
    
	 
        ขอรบกวนสอบถามเรื่องการเขียนฟังก์ชั่นวันที่จาก ไทย ไป อังกฤษนะคะ     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (PHP) 
$th_Mth=['', 'มกร', 'กุม', 'มีน','เมษ','พฤษ','มิถ','กรก','สิง','กัน','ตุล','พฤศ','ธัน'];
list($d, $mt,$y) = explode(' ', $resource);
$en_mth = substr( 100 + array_search(substr($mt,0,3), $th_mth), 1);
$y -= 543; 
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-03-11 19:28:15 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   ตอบความคิดเห็นที่ : 1 เขียนโดย : Chaidhanan เมื่อวันที่ 2019-03-11 19:28:15 
รายละเอียดของการตอบ ::
  ขอบคุณมากคะ ขอโทษทีที่มาตอบช้านะคะ พอดีโดนไปทำงานต่างจังหวัดหลายวัน วันนี้มาลองทำแล้วลองไปปรับใช้ มาดูโค๊ดที่ทำไว้กับโค๊โที่พี่Chaidhanan แนะนำมา แล้วเอาไปปรับไม่ค่อยถูกคะ อยากขอคำแนะนำเพิ่มเติมได้ไหมคะ ว่าควรทำยังไงดี แนบโค๊ดที่เกี่ยวข้องมาให้ดูคะ
 Code (PHP)form.php
<div class="form-row">
                                <div class="form-group col-md-6">
                                    <label class="h6">วัน-เวลาที่เกิดเหตุ</label>
                                    <div class="input-group mb-3">
									<input  type="text" class="form-control" data-provide="datepicker" id="datetimeevent" name="datetimeevent" data-date-language="th-th" value="<?php echo DateThai($strDate); ?>" >
                                        <div class="input-group-append">
                                            <span class="input-group-text" id="basic-addon2"><i class="fas fa-calendar-alt"></i></span>
                                        </div>
                                    </div>
                                </div>
Code (PHP)function.php
function DateThai($strDate)
	{
		$strYear = date("Y",strtotime($strDate))+543;
		$strMonth= date("n",strtotime($strDate));
		$strDay= date("j",strtotime($strDate));
		$strMonthCut = Array("","มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม");
		$strMonthThai=$strMonthCut[$strMonth];
		return "$strDay $strMonthThai $strYear";
	}
	$strDate = date('d-m-Y');
Code (JavaScript)script.js
$(function(){
    $('#datepicker').datepicker({
        format:'dd M yyyy',
    });})
function myFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("mySearch");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myMenu");
  li = ul.getElementsByTagName("li");
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
จากไฟล์ form.php ตอนนี้ส่งค่า datetimeevent = 06 มีนาคม 2562 อันนี้ออกมาให้คะ
 
รบกวนทีนะคะ
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-03-18 10:07:17 | 
                        By :
                            bananashy | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองปรับๆ เองมาเป็นแบบนี้คะ ไม่รู้ผิดถูกไหมหลักการ แต่ได้ผลลัพธ์แบบที่ต้องการเลยคะ 
 
Code (PHP) 
/*convert format date 02 มีนาคม 2562 to 2019-03-02*/
 list($d, $mt,$y) = explode(' ', $datetimeevent);
 $y -= 543; 
 if($mt == "มกราคม"){  $mt1 = '01'; }
 if($mt == "กุมภาพันธ์"){  $mt1 = '02'; }
 if($mt == "มีนาคม"){  $mt1 = '03'; }
 if($mt == "เมษายน"){  $mt1 = '04'; }
 if($mt == "พฤษภาคม"){  $mt1 = '05'; }
 if($mt == "มิถุนายน"){  $mt1 = '06'; }
 if($mt == "กรกฎาคม"){  $mt1 = '07'; }
 if($mt == "สิงหาคม"){  $mt1 = '08'; }
 if($mt == "กันยายน"){  $mt1 = '09'; }
 if($mt == "ตุลาคม"){  $mt1 = '10'; }
 if($mt == "พฤศจิกายน"){  $mt1 = '11'; }
 if($mt == "ธันวาคม"){  $mt1 = '12'; }
 $datetimeevent = $y.'-'.$mt1.'-'.$d;
 /*---------------*/
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-03-18 10:37:44 | 
                        By :
                            bananashy | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แบบไหนก็ได้ครับ แต่ใส่ else เข้าไปด้วยครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-03-18 11:49:49 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 อธิบายโค๊ดให้เข้าใจซักหน่อย เพื่อจะได้ Optmize ความเร็วของโค๊ดเพิ่มขึ้น (แม้สมัยนี้เครื่องมันจะเร็วจนแทบไม่ต้องสนใจก็ได้) 
Code (PHP) 
$th_Mth=['', 'มกร', 'กุม', 'มีน','เมษ','พฤษ','มิถ','กรก','สิง','กัน','ตุล','พฤศ','ธัน'];
// กำหนดชื่อเพื่อใช้ในการค้นหาให้สั้นที่สุดเท่าเที่จะสั้นได้ เพื่อที่จะได้ไม่ต้องเปรียบเทียบยาวเกินไป
// ที่ต้องใช้ 3 ตัว เพราะ พฤษภา กับ พฤศจิกา
list($d, $mt,$y) = explode(' ', $resource);
// แบ่งข้อมูล วัน เดือน ปี
$mt_srh = substr($mt,0,3);
// อ่านค่าเดือนเพื่อใช้ในการค้นหา 3 ตัวอักษร เท่ากับ list ที่ทำเอาไว้
$mth_position = array_search($mt_srh, $th_mth);
// หาตำแหน่งเดือน ด้วย function array_search
$en_mth = substr( 100 + $mth_position, 1);
// แปลงค่าตัวเลขให้อยู่ในรูป เดือน 2 digits
$y -= 543; 
// แปลงปี พศ เป็น คศ
 
 
พยายามเปรียบเทียบด้วยอักษร ให้สั้นที่สุดเท่าที่ทำได้ ยิ่งเปรียบเทียบยาว ยิงช้า                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-03-19 08:03:27 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
      		  
	
     | 
   
 
                 |