1 ตุลาคม 2555

PHP หาเดือนก่อนหน้า และ ย้อนหลัง 1 เดือน

<?php
function get_month($mm="") { 
 $_month_name = array(
    "01"=>"มกราคม",
    "02"=>"กุมภาพันธ์",
    "03"=>"มีนาคม",  
    "04"=>"เมษายน",
    "05"=>"พฤษภาคม",
    "06"=>"มิถุนายน",  
    "07"=>"กรกฎาคม",
    "08"=>"สิงหาคม",
    "09"=>"กันยายน",  
    "10"=>"ตุลาคม",
    "11"=>"พฤศจิกายน",
    "12"=>"ธันวาคม");
    $monthN= $_month_name[$mm];
    return $monthN;
}



$date =date('Y-m-d');
$nextmonth =date('Y-m-d', strtotime("+1 month")); //+ เดือนหน้า
$lastmonth =date('Y-m-d', strtotime("-1 month")); //- เดือนที่แล้ว

$month = date("m",strtotime($date));
$nextmonth = date("m",strtotime($nextmonth));
$lastmonth = date("m",strtotime($lastmonth));

echo "วันที่ปัจจุบัน : ".$date."<br>";
echo "<hr />";
echo "เดือนก่อนหน้า : ".get_month($lastmonth)."<br>";
echo "เดือนปัจจุบัน : ".get_month($month)."<br>";
echo "เดือนหน้า : ".get_month($nextmonth)."<br>";
?>

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

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