Forum Moderators: coopster

Message Too Old, No Replies

Comparing dates

Problems comparing string dates

         

Weirfire

11:07 pm on Oct 20, 2004 (gmt 0)

10+ Year Member



I'm trying to compare 2 dates in my PHP code so that I can delete old entries in the database. The problem I have is that the string dates are not comparing the way I am expecting them to. Any ideas?

<?php

$Date = Strtotime(date("j/n/Y - g:i a"));

$resulta = mysql_query("SELECT * FROM LoggedIn")
or die("SELECT Error: ".mysql_error());

while ($get_info = mysql_fetch_row($resulta)){
$i=0;
foreach($get_info as $field)
{
if($i==0){
$MID=$field;
}

if($i==2)
{
$ExpDate=Strtotime($field);

if($ExpDate<$Date)
{
mysql_query("DELETE FROM LoggedIn WHERE MID='$MID'");
}
}
$i++;
}
}

?>

jatar_k

7:23 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hmm

>> the string dates are not comparing the way I am expecting them to

what exactly is happening? Have you looked at the timestamps to see what you are getting?

I would change this

$Date = Strtotime(date("j/n/Y - g:i a"));

to

$Date = time();

no point in Strtotime(date( if you are just looking for NOW. Though if the expiry time isn't NOW and you want to give it some datetime from the past then use mktime to create it.

is the value of $field getting properly assigned? I would do this first

echo '<p>ExpDate',$ExpDate;
echo '<br>Date',$Date;
if($ExpDate<$Date) echo '<p>Date is higher';
else echo '<p>ExpDate is higher';

then you can eyeball everything and check the values