Jump to content

Recommended Posts

What is the proper way to reference  he birthday variable I am calling from the table in my code on line 44? My goal here is to tell each member exactly how hold they are.  The birthday is in the table as yyyy-mm-dd.  Am I some how confusing the php and mysql dates?  Not sure what my issue is right now.  The code is telling every user that they are 37 years old or so.  Not good.....

 

<?php

session_name ('YourVisitID');
session_start(); // Start the session.

// If no session value is present, redirect the user.
if (!isset($_SESSION['user_id'])) {

// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
	$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/index.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}

$page_title = 'Brothers';
include ('includes/header.html');


// Page header.
echo '<h1 id="mainhead">Brothers</h1>';

require_once ('includes/mysql_connect.php'); // Connect to the db.

// Make the query.
$query = "SELECT birthday, CONCAT_WS(' ', first_name, last_name) AS name FROM brothers ORDER BY last_name, first_name ASC";	
$result = mysql_query ($query); // Run the query.

if  (!$result) {
    echo "The Query:
$query
Produced the error:
".mysql_error();
    exit;
}

$starttime=time();
//standard this is just today's date
// the start time can change to =strtotime($endtime);
$endtime = birthday;
//$endtime can be any format as well as it can be converted to secs

$timediff = $starttime-$endtime;
$years=intval($timediff/31557600);
$remain=$timediff%31557600;
$days=intval($remain/86400);
$remain=$timediff%86400;
$hours=intval($remain/3600);
$remain=$remain%3600;
$mins=intval($remain/60);
$secs=$remain%60;

//$days is what you need
// this is just an output that tells the difrence
$timediff=$years . 'years' . $days . 'days' . $hours . 'hr' . $mins . 'min'. $secs . 's';




// Table header.
echo '<table align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Name</b></td>

<td align="left"><b>Birthday</b></td>

<td align="left"><b>Days</b></td>

</tr>';

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
	<td align="left">' . $row['name'] . '</td>
	<td align="left">' . $row['birthday'] . '</td>	
	<td align="left">' . $timediff . '</td>

</tr>
';
}

echo '</table>';

mysql_free_result ($result); // Free up the resources.	

mysql_close(); // Close the database connection.



include ('includes/footer.html'); // Include the HTML footer.
?>

If you are SELECTing more than 1 row at a time, then you should do a loop to run through the result rows.. Something like this:

 

while ($row=mysql_fetch_assoc($result)){

  $birthday = $row['birthday'];

  $name = $row['name'];

  // do some calculations here and save to array

}

 

Hope that helps!

 

I found them.  I am not being a jerk, but I am unclear where it says no triple posts.  I searched the page for triple, three, and 3.  I am just curious because I am wondering if this is a rule or just general courtesy on this board.

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.