Jump to content

date calculation and displaying


sofia403

Recommended Posts

I was wondering if someone could help me out with calculating and displaing dates. Below i posted html of my page and also php for it. It has a field called "DATE1" thats where visitors input the date, after that it gets posted to db and i pull it and disply it in a grid on my site. I would also like to have a column that shows time elapsed between the "DATE1" and current date.

 

I sorta figured how to calculate the dates using the php, but really stuck on how do i actually display it now. from what i been told so far i shouldn't create a new column for that, instead calculate and display the values on the fly, could some step me through this please, or get me started... thanks so much!

 

my html

<form id="signupForm" method="POST" action="processform.php">
  <div>
    <fieldset>
      <legend>Signup Form</legend>
      <label for="Country">*Country:</label>
      <select name="Country">
      <option value="">-----</option>
      <option value="Canada">Canada</option>
      <option value="UK">UK</option>
      <option value="France">France</option></select></div></p>

      <p><label for="Nationality">*Nationality:</label>
      <div><select name="Nationality">
      <option value="">-----</option>
      <option value="Afghanistan">Afghanistan</option>
      <option value="Albania">Albania</option>
      <option value="Algeria">Algeria</option></select></div></p>

      <p><label for="Province">*Province:</label>
      <div><select name="Province">
      <option value="">-----</option>
      <option value="British Columbia">British Columbia</option>
      <option value="Alberta">Alberta</option>
      <option value="Saskatchewan">Saskatchewan</option></select></div></p>
      
      <p><label for="DATE1">*Date:</label>
      <div><select name="day" id="Date1" class="regularfont"><option value="" selected="selected"></option>
      <option value="1">01</option>
      <option value="2">02</option>
      <option value="3">03</option></select>

      <select name="month" id="Date2" class="regularfont"><option value="" selected="selected"></option>
      <option value="1">January</option>
      <option value="2">February</option>
      <option value="3">March</option></select>

      <select name="year" id="Date3" class="regularfont"><option value="" selected="selected"></option>
      <option value="2014">2014</option>
      <option value="2013">2013</option>
      <option value="2012">2012</option></select>
    </fieldset>
  </div>
  <p>
    <div align="center"><input type="submit" name="submit" value="Submit your entry"></div>
  </p>
</form>
<p>

 

<?PHP
$hostname = "localhost";
$db_user = "";
$db_password = "";
$database = "";
$db_table = "";
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);

 

//here i use this to calculate the date but really unsure how to further display the results

if (isset($_REQUEST['submit'])) {
    // Get current time, or input time like in $date2
    $date1 = time();

    // Get the timestamp of DATE1
    $date2 = mktime(0,0,0,date($_POST['month']),date($_POST['day']),date($_POST['year']));

$dateDiff = $date1 - $date2;
    $TotalTime = floor($dateDiff/(60*60*24));
    echo "$TotalTime";
}

for posting to db

  if(isSet($_POST['submit'])) {
    $country     = mysql_real_escape_string($_POST['Country']);
    $nationality = mysql_real_escape_string($_POST['Nationality']);
    $province    = mysql_real_escape_string($_POST['Province']);
    $date        = $_POST['day'].'-'.$_POST['month'].'-'.$_POST['year'];

    $myQuery = "INSERT INTO {$db_table} (`Country`,`Nationality`,`Province`,`DATE1`) VALUES ('{$country}','{$nationality}','{$province}','{$date}')");

    if(mysql_query($myQuery)) {
      echo 'Record inserted.';
    } else {
      echo 'An error has occurred: '.mysql_error();
    }

  }
?>

Link to comment
https://forums.phpfreaks.com/topic/236380-date-calculation-and-displaying/
Share on other sites

thanks for reply,

 

actually i can see the date difference as i want it to, my php does calculate it, but right now its shows it to me after i submit the form, but i want the value to display in my grid on the page. im just unsure how i can pass it on and display the date difference on the "fly".

 

makes sense? any ideas?

 

thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.