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