2008/10/03

เปลี่ยนวันที่ในรูปแบบที่ต้องการง่ายๆ (php)

สร้าง file ชื่อ date.php แล้ว copy code ไปไส่
//// date.php

function datethai($date) {
$day = substr("$date",8,2);
$month = substr("$date",5,2);
$month = (int)$month - 1;
$year = substr("$date",0,4);
$year = $year + 543;
$thaimonth = array('มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม');
$month = $thaimonth[$month];
return "วันที่ ".(int)$day." ".$month. " พ.ศ. " .$year;
}
function dateint($date) {
$day = substr("$date",8,2);
$month = substr("$date",5,2);
$year = substr("$date",0,4);
$year = ($year + 543) - 2500 ;
return $day . "/" . $month . "/" . $year ;
}
function datethaimini($date) {
$day = substr("$date",8,2);
$month = substr("$date",5,2);
$month = (int)$month - 1;
$year = substr("$date",0,4);
$year = $year + 543;
$thaimonth = array('มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน',
'กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม');
$month = $thaimonth[$month];
$hour = substr("$date",11,2);
$minute = substr("$date",14,2);
return (int)$day." ".$month. " " .$year;
}

function date_m_d_y($date) {
$day = substr("$date",8,2);
$month = substr("$date",5,2);
$month = (int)$month - 1;
$year = substr("$date",0,4);

$thaimonth = array('Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec');
$month = $thaimonth[$month];
$hour = substr("$date",11,2);
$minute = substr("$date",14,2);
return $month." ".(int)$day. ", " .$year;
}

?>

//show.php

include("date.php");

$date_old =2008-01-01

$date1=datethai($date_old);
$date2=dateint($date_old);
$date3=datethaimini($date_old);
$date3=date_m_d_y($date_old);

echo"$date1";
echo"$date2";
echo"$date3";
echo"$date4";
?>

Output

วันที่ 1 มกราคม 2551
01/01/51
1 มกราคม 2551
Jan 1 2008

No comments: