วันจันทร์ที่ 23 เมษายน พ.ศ. 2561

PHP วันนี้ เมื่อวาน ปีที่แล้ว แปลงให้เป็นแบบ พ.ศ.

<?php
$month_name[1]="มกราคม";
$month_name[2]="กุมภาพันธ์";
$month_name[3]="มีนาคม";
$month_name[4]="เมษายน";
$month_name[5]="พฤษภาคม";
$month_name[6]="มิถุนายน";
$month_name[7]="กรกฎาคม";
$month_name[8]="สิงหาคม";
$month_name[9]="กันยายน";
$month_name[10]="ตุลาคม";
$month_name[11]="พฤศจิกายน";
$month_name[12]="ธันวาคม";

$today = date("Ymd"); //แสดงวันปัจจุบัน
$ydate = date("Ymd", strtotime("-1 days")); //แสดงเมื่อวาน - 1 
$yweek = date("Ymd", strtotime("-1 weeks")); //แสดงอาทิตย์ที่แล้ว - 1 
$ymonth = date("Ymd", strtotime("-1 months")); //แสดงเดือนที่แล้ว - 1 
$yyear = date("Ymd", strtotime("-1 years")); //แสดง ปีที่แล้ว - 1
//แยกวัน-เดือน-ปี ออก

$da=substr($today,6,2);
$mo=round(substr($today,4,2)); //แปลงเดือนให้เป็นตัวเลข
$ye=substr($today,0,4)+543;  //แปลง ค.ศ. ให้เป็น พ.ศ. 

echo $da."-".$month_name[$mo]."-".$ye; //ผลลัพธ์ 24-เมษายน-2561

?>

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

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