วันอาทิตย์ที่ 25 มิถุนายน พ.ศ. 2560

วัน เวลา ใน PHP

กำหนดให้แสดงปีแบบไทย ใน PHP
$y_now = ROUND(date("y", mktime(date("Y"))));
$nows = $y_now + 43;  //แสดง 60
$nows1 = $y_now + 42; //แสดง 59
$nows2 = $y_now + 41; //แสดง 58
$nows3 = $y_now + 40;
$nows4 = $y_now + 39;
$nows5 = $y_now + 38;


เขียน Function แสดงวันเดือนปี แบบไทย

$thai_month_short=array(  
    "0"=>"",  
    "1"=>"ม.ค.",  
    "2"=>"ก.พ.",  
    "3"=>"มี.ค.",  
    "4"=>"เม.ย.",  
    "5"=>"พ.ค.",  
    "6"=>"มิ.ย.",  
    "7"=>"ก.ค.",  
    "8"=>"ส.ค.",  
    "9"=>"ก.ย.",  
    "10"=>"ต.ค.",  
    "11"=>"พ.ย.",  
    "12"=>"ธ.ค."                  
);  
$thai_month=array(
    "0"=>"",
    "1"=>"มกราคม",
    "2"=>"กุมภาพันธ์",
    "3"=>"มีนาคม",
    "4"=>"เมษายน",
    "5"=>"พฤษภาคม",
    "6"=>"มิถุนายน",
    "7"=>"กรกฎาคม",
    "8"=>"สิงหาคม",
    "9"=>"กันยายน",
    "10"=>"ตุลาคม",
    "11"=>"พฤศจิกายน",
    "12"=>"ธันวาคม"                
);
function thai_date($time){   //แสดง 25 มิ.ย. 2560
    global $thai_month_short;  
    $thai_date_return = date("j",$time);   // - 1  เพื่อให้แสดงข้อมูลเมื่อวาน
    $thai_date_return.=" ".$thai_month_short[date("n",$time)];  
    $thai_date_return.= " ".(date("Y",$time)+543);  
    return $thai_date_return;  
}

function thai_fdate($time){    //แสดงแบบ 25 มิถุนายน 2560
    global $thai_month;  
    $thai_date_return = date("j",$time);    // - 1  เพื่อให้แสดงข้อมูลเมื่อวาน
    $thai_date_return.=" ".$thai_month[date("n",$time)];  
    $thai_date_return.= " ".(date("Y",$time)+543);  
    return $thai_date_return;  
}

$mydt = time();

นำไปใช้งาน

echo thai_date($mydt)  //แสดง 25 มิ.ย. 2560

echo thai_fdate($mydt) //แสดงแบบ 25 มิถุนายน 2560

ไม่มีความคิดเห็น:

แสดงความคิดเห็น