计算更改周和月的开始和结束?

时间:2013-06-17 18:20:27

标签: php sql date time calendar

目前我正在使用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,并谷歌寻找这个问题的解决方案,但还没找到。

1 个答案:

答案 0 :(得分: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'];
}