我正在比较两个日期From_date
和To_date
我的数据库表行。
逻辑是: 如果用户已经在这两个日期(从文本框收到的日期)之间设置了预算,则返回错误消息Budget already set for this from-date & to-date
。
我将我的日期转换为 Unix 时间戳更容易。
在尝试此操作之前,我参考了此链接: 1. 如何检查起始日期和截止日期之间的两个日期 2. 测试两个整数范围是否重叠的最有效方法是什么?
我尝试了以下代码:但我没有得到结果。它总是向我显示错误的信息,即 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'; //
}
} ?>