ฟังชั่น ระยะห่างของเวลา



function getTimeDiffString(timestamp1, timestamp2, state )
    local timeDiff = math.abs(timestamp2 - timestamp1)
    local years = math.floor(timeDiff / (365 * 24 * 60 * 60))
    timeDiff = timeDiff - (years * 365 * 24 * 60 * 60)
    local months = math.floor(timeDiff / (30 * 24 * 60 * 60))
    timeDiff = timeDiff - (months * 30 * 24 * 60 * 60)
    local days = math.floor(timeDiff / (24 * 60 * 60))
    timeDiff = timeDiff - (days * 24 * 60 * 60)
    local hours = math.floor(timeDiff / (60 * 60))
    timeDiff = timeDiff - (hours * 60 * 60)
    local minutes = math.floor(timeDiff / 60)
    timeDiff = timeDiff - (minutes * 60)
    local seconds = timeDiff
 
    local yearString = years .. " ปี "
    local monthString = months .. " เดือน "
    local dayString = days .. " วัน "
    local hourString = hours .. " ชั่วโมง "
    local minuteString = minutes .. " นาที "
    local secondString = seconds .. " วินาที"

    local result = ""
    if not state then
        if years > 0 then
            result = yearString .. monthString
        elseif months > 0 then
            result = monthString .. dayString
        elseif days > 0 then
            result = dayString .. hourString
        elseif hours > 0 then
            result = hourString .. minuteString
        else
            result = minuteString .. secondString
        end
    elseif state == 1 then
        result = ""
        if years > 0 then
            result = { { text = "year", value = years }, { text = "months", value = months }, { text = "days", value = days }, { text = "hours", value = hours }, { text = "minutes", value = minutes }, { text = "seconds", value = seconds } }
        elseif months > 0 then
            result =  { { text = "months", value = months }, { text = "days", value = days }, { text = "hours", value = hours }, { text = "minutes", value = minutes }, { text = "seconds", value = seconds } }
        elseif days > 0 then
            result = { { text = "days", value = days }, { text = "hours", value = hours }, { text = "minutes", value = minutes }, { text = "seconds", value = seconds } }
        elseif hours > 0 then
            result = { { text = "hours", value = hours }, { text = "minutes", value = minutes }, { text = "seconds", value = seconds } }
        else
            result = { { text = "minutes", value = minutes }, { text = "seconds", value = seconds } }
        end
    end
 
    return result
end

-- ตัวอย่างเวลา
local datetime_str1 = "28-03-2023 10:30:00"
local datetime_str2 = "05-01-2020 02:30:05"

function datetimeToTimestamp ( datetime_str )
    return os.time({year=tonumber(datetime_str:sub(7,10)), month=tonumber(datetime_str:sub(4,5)), day=tonumber(datetime_str:sub(1,2)), hour=tonumber(datetime_str:sub(12,13)), min=tonumber(datetime_str:sub(15,16)), sec=tonumber(datetime_str:sub(18,19))})
end

local timeData = getTimeDiffString( datetimeToTimestamp ( datetime_str2 ), datetimeToTimestamp ( datetime_str1 ), 1 )
outputChatBox( toJSON(  timeData  ) )

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

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