|
|
|
คำนวณระยะห่าง (วัน) อย่างไรคะ อยากเขียน php ให้หาระยะเวลาเป็นวัน (ตามตัวอย่างเป็น 2 วัน) ต้องเขียนยังไงคะ |
|
|
|
|
|
|
|
Code (PHP)
<?php
function DateDiff($strDate1,$strDate2)
{
return (strtotime($strDate2) - strtotime($strDate1))/ ( 60 * 60 * 24 ); // 1 day = 60*60*24
}
function TimeDiff($strTime1,$strTime2)
{
return (strtotime($strTime2) - strtotime($strTime1))/ ( 60 * 60 ); // 1 Hour = 60*60
}
function DateTimeDiff($strDateTime1,$strDateTime2)
{
return (strtotime($strDateTime2) - strtotime($strDateTime1))/ ( 60 * 60 ); // 1 Hour = 60*60
}
echo "Date Diff = ".DateDiff("2008-08-01","2008-08-31")."<br>";
echo "Time Diff = ".TimeDiff("00:00","19:00")."<br>";
echo "Date Time Diff = ".DateTimeDiff("2008-08-01 00:00","2008-08-01 19:00")."<br>";
?>
|
|
|
|
|
Date :
2009-10-14 15:22:10 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ของผมเป็น datetime เลยคำนวนชั่วโมงกับเวลาออกมาด้วยครับ
Code (PHP)
<?
function CalDate($time1,$time2){
//Convert date to formate Timpstamp
$time1=strtotime($time1);
$time2=strtotime($time2);
//$diffdate=$time1-$time2
$distanceInSeconds = round(abs($time2 - $time1)); //จะได้เป็นวินาที
$distanceInMinutes = round($distanceInSeconds / 60); //แปลงจากวินาทีเป็นนาที
$days = floor(abs($distanceInMinutes / 1440));
$hours = floor(fmod($distanceInMinutes, 1440)/60);
$minutes = floor(fmod($distanceInMinutes, 60));
return " Days:".$days ." Hour:" .$hours ." Minute:".$minutes;
}
?>
|
|
|
|
|
Date :
2009-10-14 15:38:45 |
By :
onizike |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบพระคุณทุกคนค่ะ
|
|
|
|
|
Date :
2009-10-14 22:07:23 |
By :
gigza |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?php
$strStartDate = "2011-08-01";
$strEndDate = "2011-08-15";
$intWorkDay = 0;
$intHoliday = 0;
$intTotalDay = ((strtotime($strEndDate) - strtotime($strStartDate))/ ( 60 * 60 * 24 )) + 1;
while (strtotime($strStartDate) <= strtotime($strEndDate)) {
$DayOfWeek = date("w", strtotime($strStartDate));
if($DayOfWeek == 0 or $DayOfWeek ==6) // 0 = Sunday, 6 = Saturday;
{
$intHoliday++;
echo "$strStartDate = <font color=red>Holiday</font><br>";
}
else
{
$intWorkDay++;
echo "$strStartDate = <b>Work Day</b><br>";
}
//$DayOfWeek = date("l", strtotime($strStartDate)); // return Sunday, Monday,Tuesday....
$strStartDate = date ("Y-m-d", strtotime("+1 day", strtotime($strStartDate)));
}
echo "<hr>";
echo "<br>Total Day = $intTotalDay";
echo "<br>Work Day = $intWorkDay";
echo "<br>Holiday = $intHoliday";
?>
Screenshot
Go to : PHP Checking Work Day , Holday ตรวจสอบวันหยุด
|
|
|
|
|
Date :
2011-12-13 05:57:57 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|