具有日期时间类型的函数

时间:2013-06-10 15:19:20

标签: php datetime

我遇到了问题,无法找到任何解决方案。 我正在尝试制作一个页面,以便在特定的剧院和特定时间内将节目添加到特定的电影中。我需要创建一个功能来检查上一个或下一个节目的时间,并检查可用性,以便为具有特定持续时间的电影添加节目

ex:电影时长2小时 显示1:1 PM 在下午2点添加节目时,会出现错误,因为下午1点的节目尚未完成。

我相信我的问题是日期时间格式!!因为我没有收到错误...页面正在运行,但数据类型输入不正确

代码:

    require('classes/show_class.php');

    $valid_show = show::check_time_validity($showdatetime,$movieid, $theaterid);

    if( $valid_show !== true )
    {
        echo $valid_show['error'];
        exit;
    }

    $querycinema=mysql_query(
        "SELECT show.show_datetime, show.theater_id, show.movie_id, theater.theater_name, movie.movie_name 
        FROM `show` 
        JOIN theater ON theater.theater_id = show.theater_id
        JOIN movie ON movie.movie_id = show.movie_id                            
        WHERE show.show_datetime='$showdatetime' AND show.theater_id='$theaterid' AND show.movie_id='$movieid'"
    );

    $checkcinema = mysql_num_rows( $querycinema );

    if( $checkcinema != 0 )
    { 
        $data = mysql_fetch_assoc( $querycinema );
        echo "Sorry, ".$showdatetime." is already been scheduled for movie ".$data['movie_name']." in theater ".$data['theater_name']."."; 
    }
    else
    {   echo "$selected_show_time";

        $insert_user = mysql_query("INSERT INTO `show` (show_datetime, movie_id, theater_id) VALUES ('$showdatetime','$movieid', '$theaterid')");

        if( $insert_user )
        {
            header("Refresh: 0;url=listallshows.php");
        }
        else
        {
            echo "error in registration".mysql_error();
        }
    }
}

show_class.php函数:

public static function check_time_validity($show_datetime,$movie_id, $theater_id)
{
$show_date = date($show_datetime);

$query = "
    (
        SELECT show_datetime 
        FROM  `show` 
        WHERE movie_id = {$movie_id} AND theater_id = {$theater_id} AND DATE( show_datetime ) =  '{$show_date}' AND  '{$show_datetime}' >= show_datetime
        ORDER BY show_datetime DESC 
        LIMIT 1
    )
    UNION
    (
        SELECT show_datetime
        FROM  `show` 
        WHERE movie_id = {$movie_id} AND theater_id = {$theater_id} AND DATE( show_datetime ) = '{$show_date}' AND  '{$show_datetime}' <= show_datetime
        ORDER BY show_datetime ASC 
        LIMIT 1
    )";

$results = mysql_query( $query );

$selected_show_time = strtotime( $show_datetime );

$show_times = array();

while( $show = mysql_fetch_assoc( $results ) )
{
    $show_time = strtotime( $show['show_datetime'] );

    if( $selected_show_time == $show_time )
    {
        return array( 'error' => 'Datetime inserted already exists' );
    }

    if( $selected_show_time > $show_time )
    {
        $show_times['time_before'] = $show_time;
    }
    elseif( $selected_show_time < $show_time )
    {
        $show_times['time_after'] = $show_time;
    }
}

$movie = new movie($movie_id);
$movie_duration = $movie->duration;

$can_begin = true;
$can_finish = true;
$errorMessage = array();

if( $show_times['time_before'] )
{
    $finish_time = $show_times['time_before'] + $movie_duration;

    if( ( $selected_show_time - $finish_time ) < '15' )
    {
        $can_begin = false;
        $errorMessage = array( 'error' => 'The datetime selected is in conflict with the previous show' );  echo "$selected_show_time"; echo"$finish_time";exit;

    }
}

if( $show_times['time_after'] )
{
    $finish_selected_time = $selected_show_time + $movie_duration;

    if( ( $show_times['time_after'] - $finish_selected_time  ) < '15' )
    {
        $can_finish = false;
        $errorMessage = array( 'error' => 'The datetime selected is in conflict with the next show' );
    }
}
if( $can_begin && $can_finish )
{
    return true;
}
else
{
    return $errorMessage;
}
    }

希望得到任何帮助,谢谢你......

2 个答案:

答案 0 :(得分:0)

我注意到的第一件事是你在show_datetime变量上使用date()并尝试将它与查询中的DATE(show_datetime)进行比较。
虽然mysql中的DATE()提取日期部分,但PHP中的date()不会这样做 看看http://www.php.net/manual/en/function.date.php,看看date()在PHP中是如何工作的。

答案 1 :(得分:0)

我认为我看到的最关键问题是你没有film length columnfilm end time ...这就是你没有得到预期结果的原因。

如果您习惯prepare and bind查询以保护自己免受SQL注入,那就太棒了。

我的2美分帮助将是:

首先将show_datetime查询分开并将其限制为1.那么您只有2个关闭的第一个值。那么你需要做的是创建一个函数,告诉你哪个值(日期)关闭了你的值(日期)。

function closest($array, $number) {

    sort($array);
    foreach ($array as $a) {
        if ($a >= $number) return $a;
    }
    return end($array) // or return NULL;
}

然后会告诉你最接近你给定时间的电影时间......但是你仍然会错过电影length value你永远不会在你的功能上传递它,我也无法在你的数据库查询或操作上看到它/ p>

$valid_show = show::check_time_validity($showdatetime,$movieid, $theaterid);

如果您没有finish timefilm lenght

,我认为评估它们是否重叠是不可能的