0

目前我正在使用 jQuery 日历插件来显示我构建的日历。所有数据都是使用 PHP 从 SQL 数据库中填写的。我正处于日历中,我需要及时开始向前和向后移动。我在一周和一个月的时间里都遇到了很多麻烦。就白天而言,这还不错。

这是我今天的代码

function startTime() {
  $today = getdate();
  $startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);}

然后我传入$startTime另一个发出 SQL 请求的函数。 $_SESSION[ 'number']如果之前没有设置,则设置为 0,否则我根据用户是点击下一个还是上一个来加 1 或减 1。endTime()几乎是相同的功能。

如果可能的话,我只想对我的startTime()endTime()功能进行更改。

这是用于计算日/周/月的凌乱且未经测试(我们的服务器关闭 atm)的代码

function startTime($type)
{
    $today = getDate();
    $startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);

    if ($type == "day")
    {
        $startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);
    }

    if ($type == "week")
    {
        if ($today['w'] == 0)
        {
            $startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
        }
        else
        {
            $day = strtotime("last Sunday");
            $today = getdate($day);
            $startTime = mktime(8, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
        }
    }

    if ($type == "month")
    {
        $startTime = mktime(8, 0, 0, $today["mon"]+($_SESSION['number']), 0, $today['year']);
    }

    return $startTime;
}

function endTime(type)
{
    $today = getdate();
    $endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);

    if ($type == "day")
    {
        $endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+$_SESSION['number'], $today["year"]);
    }

    if ($type == "week")
    {
        if ($today['w'] == 6)
        {
            $endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
        }
        else
        {
            $day = strtotime("next Saturday");
            $today = getdate($day);
            $endTime = mktime(20, 0, 0, $today["mon"], $today["mday"]+($_SESSION['number'] * $_SESSION['multiplier']), $today['year']);
        }
    }

    if ($type == "month")
    {
        $day = strtotime("next month - 1 hour");
        $today = getdate($day);
        $endTime = mktime(8, 0, 0, $today["mon"]+($_SESSION['number']), $today["mday"], $today['year']);
    }

    return $endTime;
}

我要感谢大家的时间,这是我的第一篇文章,所以如果我的帖子中有任何不良做法,请告诉我。我搜索了stackoverflow,并在谷歌上搜索了解决此问题的方法,但还没有找到。

4

1 回答 1

0

对于代码审查的未来参考,请使用https://codereview.stackexchange.com/如果您在获取代码以工作时遇到问题,请在http://stackoverlow.com上发布。我将使用strtotime而不是mktime创建一个返回开始时间和结束时间的函数:

function getTimes($type) {
    $end_number = $_SESSION['number'] + 1;
    $extra = '-1 day';
    if ($type == "day") {
        $end_number = $_SESSION['number'];
        $extra = '';
        $date_start = date('Y-m-d');
    } elseif ($type == 'week') {
        $date_start = date('Y-m-d',strtotime('now -'.date('w').' days'));
    } elseif ($type == 'month') {
        $date_start = date('Y-m-01');
    }
    $operator = '+';
    if($_SESSION['number'] < 0)  $operator = '';
    $startTime = strtotime("$date_start 08:00:00 {$operator}{$_SESSION['number']} {$type}s");
    $endTime = strtotime("$date_start 20:00:00 {$operator}{$end_number} {$type}s $extra");

    return array('startTime'=>$startTime,'endTime'=>$endTime);
}

我想如果您需要两个不同的功能,您可以像这样添加它们:

function startTime($type) {
    $times = getTimes($type);
    return $times['startTime'];
}
function endTime($type) {
    $times = getTimes($type);
    return $times['endTime'];
}
于 2013-06-17T19:02:47.177 回答