title: mySQL/PHP - How do I post a creation date to a new table entry?
body: <p>Cutting to the chase, I'm working on a <strong>forum-like website using PHP, Bootstrap and mySQL</strong> as part of a university assignment.</p>
  
  <p>I managed to get some of the basics working - creating new user accounts as well as new threads to be viewed by the general public.</p>
  
  <p>Sadly, there a lot of things I don't know how to do - one of them, in question, happens to be <strong>recording the current date whenever I create a new user account or forum thread.</strong></p>
  
  <p>For instance, I want each new record under the <em>users</em>table (storing data of all existing user accounts in mySQL) to have their own creation dates, the exact date they were made.</p>
  
  <p><strong>Here's what I tried so far...</strong></p>
  
  <p>The following piece of code is the top of my user registration file in PHP format.</p>
  
  <pre><code>&lt;?php require_once('../private/initialize.php'); ?&gt;
  
  &lt;?php
  if(is_post_request()) {
      $user = [];
      $user['username'] = $_POST['username'] ?? '';
      $user['email'] = $_POST['email'] ?? '';
      $user['password'] = $_POST['password'] ?? '';
      $user['confirm_password'] = $_POST['confirm_password'] ?? '';
      $mysqldate = $_POST['joined_date'] ?? '';
  
  
      $result = insert_user($user); //Keep this function in mind, readers
      if($result === true) {
          $new_id = mysqli_insert_id($db);
          log_in_user($user);
          $_SESSION['message'] = 'You successfully created a new account.';
          redirect_to(url_for('/index.php'));
      } else {
          $errors = $result;
      }
  } else {
          $user = [];
          $user['username'] = '';
          $user['email'] = '';
          $user['password'] = '';
          $user['confirm_password'] = '';
          $user['joined_dated'] = '';
      }
  ?&gt;
  </code></pre>
  
  <p><strong>To put everything into context,</strong> my website is structured into two folders:
  'public' (containing files to be accessed by the general public) and 'private' (files containing common functions used across the entire site accessed with the require and include PHP functions. </p>
  
  <p>The <strong>initialize.php</strong> file that the require_once line refers to contains a host of <em>require_once</em> lines that call up the rest of those files I mentioned. Without it, the site fails to function.</p>
  
  <p><strong>Note:</strong> <em>I have edited the code to only show the information relevant to this post.</em></p>
  
  <pre><code>&lt;?php
  
    session_start(); // turn on sessions 
  
    require_once('query_functions.php');
  
    $db = db_connect();
    $errors = [];
  
  ?&gt;
  </code></pre>
  
  <p><strong>query_functions.php</strong></p>
  
  <pre><code>function insert_user($user) {
          global $db;
  
          $errors = validate_user($user);
          if (!empty($errors)) {
              return $errors;
          }
  
          $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
          $phpdate = strtotime( $mysqldate );
  
          $hashed_password = password_hash($user['password'], PASSWORD_BCRYPT);
  
          $sql = "INSERT INTO users ";
          $sql .= "(username, email, hashed_password, joined_date) ";
          $sql .= "VALUES (";
          $sql .= "'" . db_escape($db, $user['username']) . "',";
          $sql .= "'" . db_escape($db, $user['email']) . "',";
          $sql .= "'" . db_escape($db, $hashed_password) . "',";
          $sql .= "'" . db_escape($db, $mysqldate) . "'";
          $sql .= ")";
          $result = mysqli_query($db, $sql);
  
          // For INSERT statements, $result is true/false
          if($result) {
              return true;
          } else {
              // INSERT failed
              echo mysqli_error($db);
              db_disconnect($db);
              exit;
          }
      }
  </code></pre>
  
  <p>These two codes,
  $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
  $phpdate = strtotime( $mysqldate );</p>
  
  <p>I tried them after reading one of the articles I found. It kind of
  recorded a date in every new user account, except those dates always show as "<em>0000-00-00</em>".</p>
  
  <p>Every other article I found so far shows a code solution that doesn't
  exactly follow the formula I wrote down (which I learned from a course on Lynda.com - <a href="https://www.lynda.com/PHP-tutorials/PHP-MySQL-Essential-Training-1-Basics/587674-2.html" rel="nofollow noreferrer">https://www.lynda.com/PHP-tutorials/PHP-MySQL-Essential-Training-1-Basics/587674-2.html</a>)</p>
  
  <p>So hopefully I've explained everything there is to know about my problem. So what am I missing? What am I doing wrong? How do I finally get the site to record the current date to put into each new user account?</p>
  
  <p>Thanks.</p>