|
 |
|
ขอความช่วยเหลือเรื่องการส่งค่า วัน เดือน ปี ครับไปเก็บในฐานข้อมูลครับค่าเดือนมันจะย้อนไปหนึ่งเดือนตลอด |
|
 |
|
|
 |
 |
|
โค้ดนี้ครับ
Code (PHP)
<?php
$d = date('d');
$m = date('m');
$month = array("มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน",
"กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
?>
<select name="sDay" id="sDay">
<option>วัน</option>
<?php foreach (range (1, 31) as $val) { ?>
<option value="<?=$val?>" <?=($val == $d) ? 'selected="selected"' : '' ?>><?=$val?></option>
<?php } ?>
</select>
<select name="sMonth" id="sMonth">
<option>เดือน</option>
<?php foreach ($month as $key=>$val) { ?>
<option value="<?=$key?>" <?=($key == $m-1) ? 'selected="selected"' : '' ?>><?=$val?></option>
<?php } ?>
</select>
<?php
$options = 2; ?>
<select name="sYear" id="sYear">
<?
for($i=0;$i<$options;$i++){
echo '<option value="' . (intval(date("Y")) + 543 + $i) . '">' . (intval(date("Y")) + 543 + $i) . '</option>';
} ?>
</select>
ปัญหาครับคือว่าผมทำฟอร์มเกี่ยวกับวันลา ครับโดยส่งค่าวัน เดือนปี ที่ลา พอคลิกบันทึกการลาแล้ว ระบบส่งค่าไปเก็บในฐานข้อมูลได้เป็นปกติ วัน เดือน ปี ถูกส่งไปเก็บในฐานครบครับแต่ปัญหาคือว่า
ค่า sMonth ที่ส่งไปเก็บนั้นมันไม่เป็นไปตามจริงนะครับ ตัวอย่างนะครับ ที่หน้าฟอร์ม วันเดือนปี ขึ้นมาว่า 16 ธันวาคม 2554 ถูกต้องครับ แต่พอเวลาส่งไปแล้วมันกับกลายเป็น 16 11 2554 ตามความเป็นจริงมันต้องเป็น 16 12 2554 ซืึ่งในตัว sMonth มันจะถูกลดไปหนึงเดือนตลอดเลยครับ ไม่ว่าจะเลือกเดือนไหน ก็จะย้่อยนไป หนึ่งเดือนตลอดไม่ทราบว่าจะแก้ไขตรงไหนครับ
ขอขอบคุณล่วงหน้าครับ
ปล.กระทู้อ้างอิงจาก https://www.thaicreate.com/php/forum/053398.html
ผมค้นเจอในนี้ครับนำไปทดลองใช้งานกับฐานข้อมูลครับ
Tag : PHP

|
ประวัติการแก้ไข 2011-12-16 14:29:04
|
 |
 |
 |
 |
Date :
2011-12-16 14:27:01 |
By :
rabit_it |
View :
996 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองปรับ array ดูค่ะ
Code (PHP)
$month = array(1=>"มกราคม",2=>"กุมภาพันธ์",3=>"มีนาคม",4=>"เมษายน",5=>"พฤษภาคม",6=>"มิถุนายน",
7=>"กรกฎาคม",8=>"สิงหาคม",9=>"กันยายน",10=>"ตุลาคม",11=>"พฤศจิกายน",12=>"ธันวาคม");
แก้บรรทัดนี้ เอา -1 ออกค่ะ
Code (PHP)
<option value="<?=$key?>" <?=($key == $m) ? 'selected="selected"' : '' ?>><?=$val?></option>
ไม่รู้ช่วยได้ไหม ลองดูนะคะ ^^
|
 |
 |
 |
 |
Date :
2011-12-16 14:43:46 |
By :
kukkuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<select name="sMonth" id="sMonth">
<option>เดือน</option>
<?php foreach ($month as $key=>$val) { ?>
<option value="<?=$key+1?>" <?=($key == $m-1) ? 'selected="selected"' : '' ?>><?=$val?></option>
<?php } ?>
</select>
|
 |
 |
 |
 |
Date :
2011-12-16 14:57:20 |
By :
000su000 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับขอบคุณมากครับคุณ kukkuk
อีกนิดนึงครับเมื่อสักครู่ไม่ได้ถามทิ้งไว้
คือว่า แล้วเวลาจะเอามาแสดงครับ เพราะในฐานมันจะเก็บเป็น 16 12 2554 เวลาแสดงผลมันจะเป็น ตั้งแต่วันที่ 16 12 2554 อยากให้มันแสดงเป็น 16 ธันวาคม 2554
ขอขอบคุณล่วงหน้าครับ
|
ประวัติการแก้ไข 2011-12-16 15:00:31
 |
 |
 |
 |
Date :
2011-12-16 14:58:47 |
By :
rabit_it |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองใช้ substr() ตัดเลขเดือน
https://www.thaicreate.com/php/php-substr.html
จากนั้นก็ใช้ switch..case แล้วค่อยเอาตัวแปร $month_str มาใช้จะได้ชื่อเดือนภาษาไทยค่ะ
Code (PHP)
switch ($month_num) //ตัวแปร $month_num คือเลขเดือนที่ substr มาค่ะ
{ case 1: $month_str = "มกราคม";
break;
case 2: $month_str = "กุมภาพันธ์";
break;
case 3: $month_str = "มีนาคม";
break;
case 4: $month_str = "เมษายน";
break;
case 5: $month_str = "พฤษภาคม";
break;
case 6: $month_str = "มิถุนายน";
break;
case 7: $month_str = "กรกฎาคม";
break;
case 8: $month_str = "สิงหาคม";
break;
case 9: $month_str = "กันยายน";
break;
case 10: $month_str = "ตุลาคม";
break;
case 11: $month_str = "พฤศจิกายน";
break;
case 12: $month_str = "ธันวาคม";
break;
}
ลองดูค่ะ ....ไม่แน่ใจว่ามีวิธีดีกว่านี้รึป่าวนะคะ ต้องรอพี่ๆเทพมาตอบ 
|
 |
 |
 |
 |
Date :
2011-12-16 15:42:08 |
By :
kukkuk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|