ใครเขียนได้บ้างค่ะ คือฉันมีตัวอย่างเป็น function vbscript ค่ะแต่อยากให้ช่วยเปลี่ยนเป็น PHP หรือ java อ่ะค่ะ ขอบคุณมาก ๆค่ะ
--------------------------------------------------------------------------------------------------------------------
Option Explicit
Public Const MyDB = "C:\db.mdb"
Public Const workingHours As Integer = 8
Dim wTemp As Variant
Dim strTime As String
Dim ttlTime As Date
Dim startDate, stopDate As String
Dim startTime, stopTime As Date
Dim startMT, stopMT, startAT, stopAT As Date
Dim numTaDay, numTaMrn, numTaAft As Date
--------------------------------------------------------------------------------------------------------------------
Function isHoliday(sDate) As Boolean
Dim nw As Workspace
Dim db As Database
Dim rs As Recordset
Dim sql As String
On Error GoTo SA_Errors
Set nw = DBEngine.Workspaces(0)
Set db = nw.OpenDatabase(MyDB, False, False)
sql = "SELECT holiday_date as hd FROM holiday WHERE holiday_date = CDate('" & sDate & "')"
Set rs = db.OpenRecordset(sql, dbOpenSnapshot, dbReadOnly)
If rs.EOF Then
isHoliday = False
Else
isHoliday = True
End If
Exit Function
SA_Errors:
isHoliday = False
Exit Function
End Function
--------------------------------------------------------------------------------------------------------------------
Function NetDay(FromDate As Date, ToDate As Date) As Integer
Dim years, months, days, m, c As Integer
Dim myDate
Dim myWeekday As Integer
Dim numWeekday, numHoliday As Integer
Dim i As Date, j As Integer, k As Integer
If ToDate < FromDate Then
NetDay = 0
Exit Function
End If
days = Day(ToDate) - Day(FromDate)
months = Month(ToDate) - Month(FromDate)
years = Year(ToDate) - Year(FromDate)
If years > 1 Or months > 12 Then
NetDay = "#Error"
Exit Function
End If
If days < 0 Or months > 0 Then
m = CInt(Month(ToDate)) - 1
If m = 0 Then m = 12
For c = m To Month(FromDate) Step -1
Select Case c
Case 1, 3, 5, 7, 8, 10, 12
days = 31 + days
Case 4, 6, 9, 11
days = 30 + days
Case 2
If (Year(ToDate) Mod 4 = 0 And Year(ToDate) _
Mod 100 <> 0) Or Year(ToDate) Mod 400 = 0 Then
days = 29 + days
Else
days = 28 + days
End If
End Select
Next c
End If
If Not (days > 1) Then
NetDay = CStr(days)
Exit Function
End If
numWeekday = 0: numHoliday = 0
For i = FromDate To ToDate
myWeekday = Weekday(i)
myDate = CStr(Format(i, "dd/mm/yyyy"))
If myWeekday = vbSunday Or myWeekday = vbSaturday Then
numWeekday = numWeekday + 1
End If
If isHoliday(myDate) Then
numHoliday = numHoliday + 1
End If
Next i
NetDay = days - numWeekday - numHoliday
End Function
--------------------------------------------------------------------------------------------------------------------
Function difWorkTimes(startDT, stopDT As Date, diffDay As Integer) As Variant
If Not IsNumeric(diffDay) Or Not IsDate(startDT) Or Not IsDate(stopDT) Then
difWorkTimes = "#Error"
Exit Function
End If
If diffDay < 0 Or stopTime = #12:00:00 AM# Then
'ttlTime = #12:00:00 AM#
'diffWorkTime = CStr(Format(ttlTime, "hh:mm")) & " hrs"
difWorkTimes = "#Error"
Exit Function
End If
If startDate = stopDate Then 'in a day
If startTime < stopMT Then
If stopTime < startAT Then 'start / stop in a morning
ttlTime = stopTime - startTime
Else 'start morning / stop afternoon
ttlTime = (stopTime - startAT) + (stopMT - startTime)
End If
Else 'start / stop in a afternon
ttlTime = stopTime - startTime
End If
End If
If startDate < stopDate Then 'stop at another day
If startTime < stopMT Then 'start in a morning
If stopTime < stopMT Then 'start in a morning / stop at noon
ttlTime = (stopTime - startMT) + numTaAft + (stopMT - startTime)
Else 'start morning / stop afternoon
ttlTime = (stopTime - startAT) + numTaAft + numTaMrn + (stopMT - startTime)
End If
Else 'start afternoon
If stopTime < startAT Then 'start afternoon/ stop morning
ttlTime = (stopTime - startMT) + (stopAT - startTime)
Else 'start afternoon / stop afternoon
ttlTime = (stopTime - startAT) + numTaMrn + (stopAT - startTime)
End If
End If
End If
difWorkTimes = ttlTime
End Function
--------------------------------------------------------------------------------------------------------------------
Function diffWorkTime(startDT, stopDT As Date, diffDay As Integer) As String
Dim wDiffDay, wDiffTime As String
Dim wReturn As Variant
If difWorkTimes(startDT, stopDT, diffDay) = "#Error" Then
diffWorkTime = "#Error"
Exit Function
End If
If diffDay > 0 Then
' If Hour(ttlTime) > workingHours Then
' diffDay = diffDay + 1
' ttlTime = ttlTime - numTaDay
' End If
wDiffDay = CStr(Format((diffDay), "###")) & " days "
wDiffTime = CStr(Format(ttlTime, "hh:mm")) & " hrs"
diffWorkTime = wDiffDay & wDiffTime
Else
diffWorkTime = "0 days " & CStr(Format(ttlTime, "hh:mm")) & " hrs"
End If
End Function
--------------------------------------------------------------------------------------------------------------------
Function netWorkHrs(startDT, stopDT As Date, diffDay As Integer) As Variant
wTemp = difWorkTimes(startDT, stopDT, diffDay)
If wTemp = "#Error" Then
netWorkHrs = "#Error"
Else
netWorkHrs = (Hour(wTemp) + (diffDay * 8)) + (Minute(wTemp) * 0.01)
End If
End Function
--------------------------------------------------------------------------------------------------------------------
Function netWorkMin(startDT, stopDT As Date, diffDay As Integer) As Integer
wTemp = difWorkTimes(startDT, stopDT, diffDay)
If wTemp = "#Error" Then
netWorkMin = 0
Else
netWorkMin = ((Hour(wTemp) + (diffDay * 8)) * 60) + Minute(wTemp)
End If
End Function
--------------------------------------------------------------------------------------------------------------------
Function intvlTime(startDT, stopDT As Date, diffDay As Integer) As Variant
ttlTime = difWorkTimes(startDT, stopDT, diffDay)
intvlTime = Hour(ttlTime) + (Minute(ttlTime) * 0.01)
End Function
--------------------------------------------------------------------------------------------------------------------
Function NetDay1(FromDate As Date, ToDate As Date) As Integer
Dim years, months, days, m, c As Integer
Dim myDate
Dim myWeekday As Integer
Dim numWeekday, numHoliday As Integer
Dim i As Date, j As Integer, k As Integer
If FromDate = Null Then
FromDate = 0
End If
If ToDate < FromDate Then
NetDay1 = 0
Exit Function
End If
days = Day(ToDate) - Day(FromDate)
months = Month(ToDate) - Month(FromDate)
years = Year(ToDate) - Year(FromDate)
If years > 1 Or months > 12 Then
NetDay1 = 0
Exit Function
End If
If days < 0 Or months > 0 Then
m = CInt(Month(ToDate)) - 1
If m = 0 Then m = 12
For c = m To Month(FromDate) Step -1
Select Case c
Case 1, 3, 5, 7, 8, 10, 12
days = 31 + days
Case 4, 6, 9, 11
days = 30 + days
Case 2
If (Year(ToDate) Mod 4 = 0 And Year(ToDate) _
Mod 100 <> 0) Or Year(ToDate) Mod 400 = 0 Then
days = 29 + days
Else
days = 28 + days
End If
End Select
Next c
End If
If Not (days > 1) Then
NetDay1 = CStr(days)
Exit Function
End If
numWeekday = 0: numHoliday = 0
For i = FromDate To ToDate
myWeekday = Weekday(i)
myDate = CStr(Format(i, "dd/mm/yyyy"))
If myWeekday = vbSunday Or myWeekday = vbSaturday Then
numWeekday = numWeekday + 1
End If
If isHoliday(myDate) Then
numHoliday = numHoliday + 1
End If
Next i
NetDay1 = days
End Function
--------------------------------------------------------------------------------------------------------------------