问题
Hi I am trying to create a simple while loop that pulls an associative array from a database in PHP. In essence I want to then do a compare on the date pulled from the database with the current date. Here is the catch. Different sections (represented in a table) have different deadlines. So I need to modify the pulled date from the database by subtracting certain amounts of days. I have done this with different variables but I get some weird results which are not the ones I have specified. Am i missing something?
$today = date("Y-m-d")
while ($query_row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$date = $query_row['DueDate'];// this is the due date from the server
$duedate = $date->format('d-m-Y'); // to output in table
$compare1 = $date;
$compare2 = $date;
$compare3 = $date;
$compare4 = $date;
$compare1->modify('-4 days');
$compare2->modify('-3 days');
$compare3->modify('-2 days');
$compare4->modify('-1 day');
echo $today.' today <br/>';
//just for debugging
echo $date ->format('Y-m-d').' due date <br/>';
echo $compare1->format('Y-m-d').' minus 4 date <br/>';
if ( $today >= $compare1 )
{
$Triginsert = 'style="background-color: #ff0000;"';
}
else
{
$Triginsert = '';
}
}
As you can see if the current date is greater than or equal to the compared date which should be the date pulled from server - 4 days then it should turn the block red else it should do nothing..
Any help would be great.
Thanks
回答1:
what's happening is that since objects are assigned by reference in php 5+, all your $compare variables are references to the same original $date object. So you are modifying the same one repeatedly instead of creating 4 separate ones. See http://www.php.net/manual/en/language.oop5.references.php . What you want instead is:
$compare1 = clone $date;
$compare2 = clone $date;
etc.
BTW what do you get when you var_dump($query_row['DueDate']) ? I was assuming it was already a date object, otherwise the ->modify() calls would error immediately, and you were saying you were getting weird behavior rather than an error message, but some of the other answers are assuming that $query_row['DueDate'] is a string so I wanted to double check.
回答2:
The data from the query will be just text not a Date object, you need to setup the object using that data to achieve what you want
$date = new DateTime($query_row['DueDate']);
The problems you are going to have are based on the form of DueDate so you may need to manipulate it first.
Here is an explanation of setting up a Date object http://www.php.net/manual/en/datetime.construct.php
回答3:
Your problem is this:
$date = $query_row['DueDate'];// this is the due date from the server
^^^^^^----string
$duedate = $date->format('d-m-Y'); // to output in table
^^^^^---using string as an object
You need to pass your $date
STRING to the DateTime system via a constructor, e.g.
$date = new DateTime($query_row['DueDate']);
THEN you can start using it as an object.
来源:https://stackoverflow.com/questions/22968400/strange-behaviour-in-a-while-loop-with-a-date-compare-in-php