如何在SC中将SCORM 2004 session_time格式转换为可用的日期时间格式

时间:2013-08-08 14:05:00

标签: php sql scorm2004

所以SCORM 2004的session_time格式如下所示

P[yY][mM][dD][T[hH][mM][s[.s]S]]

方括号中的选项是可选的,但它必须包含至少一个选项。

格式示例为PT0H0M47S

这意味着0小时0分47秒。

该值在MSSQL数据库中存储为varchar。

我需要能够将此字符串格式转换为可用格式,以便我可以将此时间添加到开始日期时间以计算结束日期时间。

我目前正在将SCORM数据从旧数据库移动到新数据库1.我可以在PHP中执行此操作并轻松格式化时间,但我更愿意在SQL中编写所有导入脚本。

对此的任何帮助将不胜感激。

编辑:

如果有帮助,这是我编写的用于处理其他用途格式的PHP函数

private function formatDuration($duration)
{
    $count = preg_match('/P(([0-9]+)Y)?(([0-9]+)M)?(([0-9]+)D)?T?(([0-9]+)H)?(([0-9]+)M)?(([0-9]+)(\.[0-9]+)?S)?/', $duration, $matches);

    if ($count)
    {
        $_years = (int) $matches[2];
        $_months = (int) $matches[4];
        $_days = (int) $matches[6];
        $_hours = (int) $matches[8];
        $_minutes = (int) $matches[10];
        $_seconds = (int) $matches[12];
    }
    else
    {
        if (strstr($duration, ':'))
        {
            list($_hours, $_minutes, $_seconds) = explode(':', $duration);
        }
        else
        {
            $_hours = 0;
            $_minutes = 0;
            $_seconds = 0;
        }
    }

    // I just ignore years, months and days as it is unlikely that a
    // course would take any longer than 1 hour
    return $_seconds + (($_minutes + ($_hours * 60)) * 60);
}

2 个答案:

答案 0 :(得分:3)

为我制定了一个解决方案。

这需要放在我的SQL函数中,我需要完成最后一点,我决定我想要什么格式。

我只更新了大约900行,这是1次更新,因此性能不是问题。

DECLARE @session_time varchar(200) = 'P50Y2M3DT4H5M6S'

DECLARE @date varchar(100) = ''
DECLARE @time varchar(100) = ''

DECLARE @year varchar(20) = '0'
DECLARE @month varchar(20) = '0'
DECLARE @day varchar(20) = '0'
DECLARE @hour varchar(20) = '0'
DECLARE @minute varchar(20) = '0'
DECLARE @second varchar(20) = '0'

-- Remove the P
SET @session_time = SUBSTRING(@session_time, 2, LEN(@session_time)-1)

-- If we have a T
IF PATINDEX('%T%',@session_time) > 0
BEGIN
    SET @date = SUBSTRING(@session_time, 0, PATINDEX('%T%',@session_time))

    SET @time = SUBSTRING(@session_time, LEN(@date + 'T') + 1, LEN(@session_time))
END
ELSE
BEGIN
    SET @time = @session_time;
END

-- Get the date parts
IF LEN(@date) > 0
BEGIN
    -- If theres a year
    IF PATINDEX('%Y%',@date) > 0
    BEGIN
        SET @year = SUBSTRING(@date, 0, PATINDEX('%Y%',@date))

        SET @date = SUBSTRING(@date, LEN(@year + 'Y') + 1, LEN(@date))
    END

    -- If theres a month
    IF PATINDEX('%M%',@date) > 0
    BEGIN
        SET @month = SUBSTRING(@date, 0, PATINDEX('%M%',@date))

        SET @date = SUBSTRING(@date, LEN(@month + 'M') + 1, LEN(@date))
    END

    -- If theres a day
    IF PATINDEX('%D%',@date) > 0
    BEGIN
        SET @day = SUBSTRING(@date, 0, PATINDEX('%D%',@date))

        SET @date = SUBSTRING(@date, LEN(@day + 'D') + 1, LEN(@date))
    END
END

-- Get the time parts
IF LEN(@time) > 0
BEGIN
    -- If theres an hour
    IF PATINDEX('%H%',@time) > 0
    BEGIN
        SET @hour = SUBSTRING(@time, 0, PATINDEX('%H%',@time))

        SET @time = SUBSTRING(@time, LEN(@hour + 'H') + 1, LEN(@time))
    END

    -- If theres a minute
    IF PATINDEX('%M%',@time) > 0
    BEGIN
        SET @minute = SUBSTRING(@time, 0, PATINDEX('%M%',@time))

        SET @time = SUBSTRING(@time, LEN(@minute + 'M') + 1, LEN(@time))
    END

    -- If theres a second
    IF PATINDEX('%S%',@time) > 0
    BEGIN
        SET @second = SUBSTRING(@time, 0, PATINDEX('%S%',@time))

        SET @time = SUBSTRING(@time, LEN(@second + 'S') + 1, LEN(@time))
    END
END

PRINT @year + ' years '
PRINT @month + ' months '
PRINT @day + ' days '
PRINT @hour + ' hours '
PRINT @minute + ' minutes '
PRINT @second + ' seconds '

答案 1 :(得分:0)

我的解决方案:

PTMHStoHHMMSS : function(val){ //custom SCORM extension to convert P[yY][mM][dD][T[hH][mM][s[.s]S]] to hh:mm:ss
        var seconds, minutes, hours;

        hours  = val.match(/(?:T)(.*)(?:[H])/i); //match everything between T & H
        if (hours.length > 0){
            hours = hours[1] * 1;
        } else {
            hours = 0;
        }

        minutes  = val.match(/(?:T.*[H])(.*)(?:[M])/i); //match everything between H & M that's after T
        if (minutes.length > 0){
            minutes = minutes[1] * 1;
        } else {
            minutes = 0;
        }

        seconds  = val.match(/(?:T.*[M])(.*)(?:[S])/i); //match everything between M & S that's after T
        if (seconds.length > 0){
            seconds = seconds[1] * 1;
        } else {
            seconds = 0;
        }
        //pad lower number with 0 if needed
        if (hours   < 10){ hours   = "0" + hours;   }
        if (minutes < 10){ minutes = "0" + minutes; }
        if (seconds < 10){ seconds = "0" + seconds; }
    return hours + ':' + minutes + ':' + seconds;
    }