Comparing dates/times - PHP, XML, strtotime
Stack Overflow Community,
I have always appreciated your wisdom and assistance. Here is my situation:
Here is a snippet of my XML file (this is a very small portion of it):
<matchup id="1" date="09/08/11" time="7:30 PM">
<away city="New Orleans">Saints</away>
<home city="Green Bay">Packers</home>
<finalscore>
<away></away>
<home></home>
</finalscore>
</matchup>
Okay, so using SimpleXML I am getting this XML data and storing it into respective PHP variables. The only thing I really need help with is the date/time section.
I am wanting to basically compare the date/time of the matchup to the current time. If the time of the matchup has already passed (or started for that matter), I want to display a different output.
So here is my PHP, but something is not working correctly here:
foreach($week->matchup as $matchup)
{
$game_time = $matchup['time'];
$game_date = $matchup['date'];
date_default_timezone_set('US/Eastern');
$game_date2 = strtotime($game_date);
$game_date3 = date('m/d/y', $game_date2);
$time_stamp = strtotime($game_time);
$check = date("g:i A", $time_stamp);
$date_now = date('m/d/y');
$time_now = date('g:i A');
if ($game_date3 >= $date_now && $check >= $time_now) {
?>
<tr>
<td class="two"><?php echo $game_date ?></td>
<td class="two">
<input type="radio" id="<?php echo $away_city ?> <?php echo $away_teamname ?>" class="radio" name="<?php echo $week_name ?>" value="<?php echo $away_city ?> <?php echo $away_teamname ?>"></input>
<?php echo $away_full ?>
</td>
<td class="two">
<input type="radio" id="<?php echo $home_city ?> <?php echo $home_teamname ?>" class="radio" name="<?php echo $week_name ?>" value="<?php echo $home_city ?> <?php echo $home_teamname ?>"></input>
<?php echo $home_full ?>
</td>
<td class="two"><?php echo $game_time ?></td>
</tr>
<?php
} else {
?>
<tr>
<td class="two"><?php echo $game_date ?></td>
<td class="two">
<span><?php echo $away_full ?></span>
</td>
<td class="two">
<spa开发者_高级运维n><?php echo $home_full ?></span>
</td>
<td class="two"><?php echo $game_time ?></td>
</tr>
<?php
}
} ?>
As you can see here, what I am trying to do is simply check if the date/time from the XML is past the time of the current time. If it is, I want to display an output WITHOUT input radio buttons. If it is not past the current time, I want to display the output WITH input radio buttons.
I think the problem is somewhere in my variable setting with strtotime and getting the XML date/time to parse properly against the server time.
I hope someone can help me figure this out! Also, any recommendations to make this more concise and secure is appreciated as well.
You can't compare date strings. You need to convert them to timestamps using strtotime
then compare them.
Like this:
$date = '09/08/11 7:30 PM';
if (strtotime($date) < time()) {
// $date is in the past
}
So in your case:
$game_time = $matchup['time'];
$game_date = $matchup['date'];
$date = "{$game_date} {$game_time}";
if (strtotime($date) < time()) {
// $date is in the past
}
I would caution against using the older date functions and suggest that you use the DateTime class. For the above you should be looking at DateTime::createFromFormat();
So you would have something like this:
$game_date2 = DateTime::createFromFormat('d/m/Y H:i:s', $game_date);
$game_date3 = new DateTime();
if($game_date2 > $game_date3) {
echo 'game yet to be played';
}
Note you need at least PHP5.2.2 to be able to compare date objects like have in my code above. See the DateTime::diff() page for more information.
精彩评论