|
 |
|
หากเราย้อนอดีตได้ เอ่อ .. พี่คะ อยากทราบว่า ถ้าเราจะเขียนโปรแกรมให้คำนวณวันที่ย้อนหลัง 3 วัน |
|
 |
|
|
 |
 |
|
เอาฟังก์ชันนี้ไปดูเป็นตัวอย่างนะครับ
//****************************เป็นตัวอย่างหาวันที่ล่วงหน้าตามที่กำหนด โดยรับค่าอากิวเมนมา 2 ตัว คือ $date กับ $n คือ จำนวนวันที่ต้องการหาเพื่มไปอีกกี่วัน ****************************
//****************************ส่วนการหาวันย้อนหลังใช้หลักการเดียวกันครับ ให้นำโค้ดไปปรับปรุงได้ครับ****************************
//****************************เพื่มวันไปอีก ตามที่กำหนด****************************
function dateadd($date,$n)//start function
{
$spirit=explode("/",$date);
$day=$spirit[0];
$month=$spirit[1];
$year=$spirit[2];
$limit_day=$day+$n;
if (($month=="1") or ($month=="3") or ($month=="5") or ($month=="7") or ($month=="8") or ($month=="10") or ($month=="12"))
{
$lastday="31";
if(($month>="12"))
{
if($limit_day>$lastday)
{
$year=$year+1;
$month=1;
$day=$limit_day-$lastday;
}
else
{
$day=$limit_day;
}
}
else
{
if($limit_day>$lastday)
{
$month+=1;
$day=$limit_day-$lastday;
}
else
{
$day=$limit_day;
}
}
}
elseif(($month=="4") or ($month=="6") or ($month=="9") or ($month=="11"))
{
$lastday="30";
if($limit_day>$lastday)
{
$month+=1;
$day=$limit_day-$lastday;
}
else
{
$day=$limit_day;
}
}
else
{
if($year % 4==0)
{
$lastday="29";
if($limit_day>$lastday)
{
$month+=1;
$day=$limit_day-$lastday;
}
else
{
$day=$limit_day;
}
}
else
{
$lastday="28";
if($limit_day>$lastday)
{
$month+=1;
$day=$limit_day-$lastday;
}
else
{
$day=$limit_day;
}
}
}
//ใส่ 0 นำหน้า เฉพาะ วัน/เดือน
$day="0" . $day;
$day=substr($day,-2);
$month="0" . $month;
$month=substr($month,-2);
return $day."/".$month."/".$year;
}//end function
|
 |
 |
 |
 |
Date :
19 มี.ค. 2548 15:48:23 |
By :
noom_programmer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็นฟังก์ชันที่เขียนขึ้นมาสำหรับคำนวณเวลาในการยืมคืนหนังสือครับ และยังมีฟังห์ชันหาผลต่างของวันอีก แต่ฟังก์ชันที่ให้ไครอบคลุมแล้ว สังเกตุดีๆ จะมีการหาวันที่สุดท้ายของเดือนรวมด้วย ......... 
|
 |
 |
 |
 |
Date :
19 มี.ค. 2548 15:52:29 |
By :
noom_programmer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่มจะตอบในกระทู้เก่า ๆ นะ ไม่น่าจะเกิน 100 กระทู้ลองค้นหาดู ตอบละเอียดด้วย
|
 |
 |
 |
 |
Date :
21 มี.ค. 2548 18:00:30 |
By :
@W_IN |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
echo date('Y-m-d', strtotime('+1 month'))."<br>";
echo date('Y-m-d',strtotime("now"))."<br>";
echo date('Y-m-d',strtotime("10 September 2000"))."<br>";
echo date('Y-m-d',strtotime("+1 day"))."<br>";
echo date('Y-m-d',strtotime("+1 week"))."<br>";
echo date('Y-m-d',strtotime("+1 week 2 days 4 hours 2 seconds"))."<br>";
echo date('Y-m-d',strtotime("next Thursday"))."<br>";
echo date('Y-m-d',strtotime("last Monday"))."<br>";
echo date("Y-m-d H:i:s", mktime(date("H"), date("i")+0, date("s")+0, date("m")+0 , date("d")+0, date("Y")+0))."<br>";
?>
|
 |
 |
 |
 |
Date :
2009-04-26 10:37:18 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|