我正在比较两个日期From_date
& To_date
与我的数据库表行。
逻辑是:如果用户已经在这两个日期(从文本框中收到的日期)之间设置了预算,则会返回错误消息Budget already set for this from-date & to-date
。
我将日期转换为Unix时间戳更容易。
在尝试此操作之前,我会参考此链接: 1. how to check two dates between from-date and to-date 2。 What's the most efficient way to test two integer ranges for overlap?
我尝试了以下代码:但我没有得到结果。
它总是向我显示错误消息即 Budget already set for this from-date & to-date
<?php if (isset($_POST['submit']))
{
include '../../../include/connection.php';
$frmdate= $_POST['frmdate'] ;
$todate=$_POST['todate'] ;
$fromTimestamp = strtotime( $frmdate );
$toTimestamp = strtotime( $todate );
function check_function($fromTimestamp,$toTimestamp)//function for checking the two dates which are rrecieved from the database table
{
$sid=$_SESSION['id'];
$result12=mysql_query("SELECT * FROM budget where creater_id = '$sid'");
while($test12 = mysql_fetch_array($result12))
{
$from_date = strtotime($test12['from_date']);//getting all the FROM-Dates_column
$to_date = strtotime($test12['to_date']);//getting all the TO-Dates_column
if((($fromTimestamp >= $from_date) && ($fromTimestamp <= $to_date)) || (($toTimestamp >= $from_date) && ($toTimestamp <= $to_date)))
{
return TRUE;
break;
}
return FALSE;
}
}
if(check_function($fromTimestamp,$toTimestamp))//Returns TRUE
{
mysql_query("INSERT INTO `budget` (heads_id,amount,from_date,to_date,creater_id,created_date,updater_id,updated_date) VALUES ('$headsid','$amount','$frmdate','$todate',$uid,NOW(),$uid,NOW())");
}
else
{
echo 'Budget already set for this from-date & to-date'; //
}
} ?>
答案 0 :(得分:0)
试试这个..
if(isset($to_date) && isset($from_date)){
if((($fromTimestamp >= $from_date) && ($fromTimestamp <= $to_date)) || (($toTimestamp >= $from_date) && ($toTimestamp <= $to_date)))
{
$val = 'Y'; //return Y in meet this condition
}
else{
$val = 'N'; // return N if meet this condition
}
}
if($val == 'Y') //Returns TRUE
{
mysql_query("INSERT INTO `budget` (heads_id,amount,from_date,to_date,creater_id,created_date,updater_id,updated_date) VALUES ('$headsid','$amount','$frmdate','$todate',$uid,NOW(),$uid,NOW())");
}
else
{
echo 'Budget already set for this from-date & to-date'; //
}
答案 1 :(得分:0)
您是否尝试使用默认的mysql日期格式2013/04/11进行比较 (年/月/日)??如果您只考虑约会,那么您必须存储您的 具有Y / m / d格式的mysql数据库中的数据也可以与之比较 一样!它效果最好(y)
1)以MySQL日期格式存储:
date("Y/m/d", strtotime($Date));
2)比较日期
<?php
if (isset($_POST['submit']))
{
include '../../../include/connection.php';
$frmdate= date("Y/m/d", strtotime($_POST['frmdate']));
$todate= date("Y/m/d", strtotime($_POST['todate']));
.. so on
}
?>
答案 2 :(得分:0)
我刚看了你的代码。
你的while循环往往只运行一次。返回FALSE; statement是while循环中的最后一个语句。因此,如果您查询返回多个结果,循环似乎意味着,您将始终只获取一个结果。在该运行之后,您的函数返回true(当第一个结果与if语句匹配时)或者在任何其他情况下返回false。
while($test12 = mysql_fetch_array($result12))
{
$from_date = strtotime($test12['from_date']);//getting all the FROM-Dates_column
$to_date = strtotime($test12['to_date']);//getting all the TO-Dates_column
if((($fromTimestamp >= $from_date) && ($fromTimestamp <= $to_date)) || (($toTimestamp >= $from_date) && ($toTimestamp <= $to_date)))
{
return TRUE;
break;
}
return FALSE;
}
关于两个陈述的最后说明
return TRUE;
break;
此处不需要中断,返回已经突破了while循环。
希望这能解决你的问题!
答案 3 :(得分:0)
function check_function($fromTimestamp,$toTimestamp)//function for checking the two dates which are rrecieved from the database table
{
$sid=$_SESSION['id'];
$result12=mysql_query("SELECT * FROM budget where creater_id = '$sid' and from_date >= $frmdate and to_date <= $todate")
if(mysql_num_rows($result12) > 0)
{
return true;
}
else
{
return false;
}
}
if(check_function($fromTimestamp,$toTimestamp)==TRUE)//Returns TRUE
{
mysql_query("INSERT INTO `budget` (heads_id,amount,from_date,to_date,creater_id,created_date,updater_id,updated_date) VALUES ('$headsid','$amount','$frmdate','$todate',$uid,NOW(),$uid,NOW())");
}
else
{
echo 'Budget already set for this from-date & to-date'; //
}
答案 4 :(得分:-1)
html格式的格式是什么? $ frmdate = $ _POST ['frmdate']; $ todate = $ _ POST ['todate'];
你的列表中的格式是什么?例如:DATE,VARCHAR,TIMESTAMP等......