sofia403 Posted May 29, 2011 Share Posted May 29, 2011 Hi, im trying to calculate and display a time elapsed from known date in db. however im having trouble displaying it. other values display no problem, just cant calculate and display date on the fly. does anyone see a problem with my query? thank you. <?php include('config.php'); $uname=$_SESSION['username']; if($_SESSION['username']){ $sql="SELECT country, nationality, passportDate FROM country WHERE uname='$uname'"; $result = mysql_query($sql) or die(mysql_error()); $passportDate _ts = strtotime($row['passportDate ']); $passportDate _str = date("M-d-Y", $passportDate _ts); $TotalTime = floor((time() - $passportDate _ts)/(60*60*24)) . ' days'; while($row=mysql_fetch_array($result)) { echo "<table border='0'> <tr> <th>Nationality</font></th> <th>Country</font></th> <th>Passport Seent Date</font></th> <th>Time Elapsed</font></th> </tr>"; echo "<tr>"; echo "<td>" . $row['nationality']."</td>"; echo "<td>" . $row['country'] . "</td>"; echo "<td>" . $row['passportDate '] . "</td>"; echo "<td>" . ('$TotalTime') . "</td>"; echo "</tr>"; } echo "</table>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237798-calculating-and-displaying-date/ Share on other sites More sharing options...
xyph Posted May 29, 2011 Share Posted May 29, 2011 You have spaces in so many places you shouldn't. You can use MySQL to perform that calculation. SELECT `country`, `nationality`, `passportDate`, DATEDIFF(NOW(), `passportDate`) as `totaltime` FROM `country` WHERE `uname`='$uname' Quote Link to comment https://forums.phpfreaks.com/topic/237798-calculating-and-displaying-date/#findComment-1221970 Share on other sites More sharing options...
sofia403 Posted May 29, 2011 Author Share Posted May 29, 2011 hi, thanks! that helped do you mean the spaces for time calculation, like _ts _str? I've noticed that too, that got inserted when i copied over the code... not sure why. Quote Link to comment https://forums.phpfreaks.com/topic/237798-calculating-and-displaying-date/#findComment-1221986 Share on other sites More sharing options...
xyph Posted May 29, 2011 Share Posted May 29, 2011 Yes, those spaces. They'd produce fatal errors, so it doesn't surprise me that they don't exist in your code. Odd that they got added. Quote Link to comment https://forums.phpfreaks.com/topic/237798-calculating-and-displaying-date/#findComment-1221987 Share on other sites More sharing options...
sofia403 Posted May 29, 2011 Author Share Posted May 29, 2011 cool thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/237798-calculating-and-displaying-date/#findComment-1221989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.