Jump to content

Current time minus timestamp from db


jacko_162

Recommended Posts

Im trying to create an IF statement for the following data from database;

 

if $record['logoffDateTime'] is older or equal to 60 days old, echo TRUE else echo FALSE.

 

this is my code snippets im working from;

 

<?php
// Define and perform the SQL query
$results = mysql_query('SELECT * FROM membertracking ORDER BY name ASC');
$results_array = array();
while ($row = mysql_fetch_array($results)) {
$results_array[$row['characterID']] = $row;
}

foreach ($results_array as $id => $record) {
$tempdate = mktime(0,0,0,date("m"),date("d")-60,date("Y"));
$tempdate = date("Y-m-d", $tempdate);

$datecheck = $record['logoffDateTime'];


if ($datecheck <= '$tempdate') {

echo "TRUE";
} else {
echo "FALSE";
};
?>
?></td>


<td><?php echo $record['name'];?></td>
<td><?php echo $record['startDateTime'];?></td>
<td><?php echo $record['logoffDateTime'];?></td>
<td><?php echo $record['location'];?></td>
</tr>
<?php } ?>

 

for some reason its returning "TRUE" for all my results, even though some of the "$record['logoffDateTime']" is older than 60 days.. what have i done wrong?

Link to comment
Share on other sites

SELECT IF(
(DATEDIFF(logoffDateTime, CURDATE()) > 60), 1, 0
) AS columnalias, membertracking.* -- You should list specific fields, not *
FROM membertracking 
ORDER BY name ASC

Not tested.

 

Run that query and do a print_r on the resulting row. (I may have the 1 and 0 backward, not sure exactly what you're going for.)

Link to comment
Share on other sites

I generally find when doing something like this, rather than trying to subtract two dates, it's easier/better to subtract 60 days from 'now' and then compare to the db records.  Eg:

SELECT
   CASE WHEN (CURRENT_TIMESTAMP-INTERVAL 60 DAY) < logoffDateTime THEN 1 ELSE 0 END as isOlderThan60Days,
   ...

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.