简体   繁体   English

PHP MSSQL使用“哪里”选择日期范围

[英]PHP MSSQL use “Where” to select a date range

I'm trying to populate a list menu where the selections will either be in the past or the future based on the gamedate. 我正在尝试填充一个列表菜单,其中的选择将基于游戏日期在过去或将来。

i want 1 menu to list games that have been played, based on the current date, and 1 menu to list games that have not been played. 我想要1个菜单基于当前日期列出已玩过的游戏,以及1个菜单列出未玩过的游戏。 basically if $gamedate>$daynow or $gamedate<=$daynow 基本上是$ gamedate> $ daynow或$ gamedate <= $ daynow

i've looked up examples online but none of my attempts have worked. 我在网上查找了示例,但我的尝试都没有成功。 perhaps i need to know how to convert $gamedate to a unix time stamp but everything i try doesn't work. 也许我需要知道如何将$ gamedate转换为unix时间戳,但是我尝试的所有方法都无法正常工作。 I've tried: strtotime, DateTime::, and using "WHERE" and "between" to create a date range 我尝试过:strtotime,DateTime ::,并使用“ WHERE”和“ between”创建日期范围

$gamedate is assigned from a MSSQL db and the field is in date format $ gamedate是从MSSQL数据库分配的,并且该字段为日期格式

my thought was to convert $gamedate to the UNIX timestamp and use that to compare to time(). 我的想法是将$ gamedate转换为UNIX时间戳,并使用它与time()进行比较。 am i correct in thinking this is an integer so using ">" or "<" would work? 我认为这是整数是正确的,所以使用“>”或“ <”可以工作吗? i'm open to other ways as well. 我也欢迎其他方式。

<?PHP
$daynow=time()

$sql="Select convert(varchar(5), gamedate, 101) AS d, * WHERE gamedate>=$daynow ORDER BY gamedate ASC"
$stmt=sqlsrv_query($conn, $sql);
while ($row=sqlsvr_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
echo"this game has not yet been played";
}
?>

or 要么

<?PHP
$sql="Select convert(varchar(5), gamedate, 101) AS d, * ORDER BY gamedate ASC"
$stmt=sqlsrv_query($conn, $sql);
while ($row=sqlsvr_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
$gamedate=$row['gamedate'];
$daynow=time();
if ($gamedate>$daynow){
echo"this game has not yet been played";}
elseif ($gamedate<=$daynow){
echo"this game has been played";}
}
?>

everything i find is how to covert a timestamp to a string 我发现的所有内容都是如何将时间戳转换为字符串

thank you 谢谢

Check this: What is the preferred format to store date/times in a SQL Server database when PHP is your primary language? 请检查以下内容: 当PHP是您的主要语言时,在SQL Server数据库中存储日期/时间的首选格式是什么? I think it would be more helpful since that quesiton is related to MSSQL 我认为这会更有用,因为该问题与MSSQL有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM