title: php/mysql: get data from one table, convert and insert in another table
body: <p>Ok I will try to explain, (I am Portuguese lol).</p>
  <p>I have a Database where I have the fallowing MySQL:</p>
  <p>Database: ad<br />
  Table: notebooks<br />
  Fields: ID, tag, so, lastlogon, lastlogh</p>
  <p>My ldap query gets data from ldap server (active directory) and stores it in my MySQL database (ad/notebooks).
  I get tag which is tag number is unique.
  I get so which is the OS installed in the notebook.
  I get lastlogh which is the last logon time stamp, and it is unique too.
  ID field is auto increment and key but I can set the tag filed to be key.</p>
  <p>I have a config_notebooks.php where I set all the variables to connect to ldap and MySQL servers.</p>
  <pre><code>&lt;?php
  /*------------------------------------------------------------------------*/
  //setting your variables
  /*------------------------------------------------------------------------*/
  //ldap host
  $host = &quot;ldap://HEICPT1VIA01.HEIWAY.NET&quot;;
  //ldap user
  $user = &quot;domain\user&quot;;
  //ldap password
  $pswd = &quot;pssw&quot;;
  //ldap base structure
  $dn = &quot;OU=NotebookM2,OU=WorkstationsM2,OU=PT1,DC=heiway,DC=net&quot;;;
  //attributs to search and get
  $attrs = array(&quot;cn&quot;,&quot;operatingsystem&quot;,&quot;lastlogon&quot;);
  //sql host
  $sqlhost=&quot;localhost&quot;; 
  //sql user
  $sqluser=&quot;root&quot;;
  //sql password
  $sqlpswd=&quot;&quot;;
  //sql database
  $database=&quot;ad&quot;;
  //sql table
  $table=&quot;notebooks&quot;;
  ?&gt;
  </code></pre>
  <p>Now the query script</p>
  <pre><code>&lt;?php
  /*------------------------------------------------------------------------*/
  //Query script
  /*------------------------------------------------------------------------*/
  include 'config_notebooks.php';
  $filter = $_POST['filter'].&quot;=&quot;.$_POST['keyword'].&quot;*&quot;;
  //connect to db
  $con = mysql_connect(&quot;$sqlhost&quot;,&quot;$sqluser&quot;,&quot;&quot;); 
  if (!$con) 
  { 
  die('Could not connect: ' . mysql_error()); 
  } 
  
  //connect to active directory
  $ad = ldap_connect($host)
    or die( &quot;Could not connect!&quot; );
    
    // Set version number
  ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
   or die (&quot;Could not set ldap protocol&quot;);
    
    // Binding to ldap server
  $bd = ldap_bind($ad, $user, $pswd)
    or die (&quot;Could not bind&quot;);
  
  $search = ldap_search($ad, $dn, $filter, $attrs)
        or die (&quot;ldap search failed&quot;);
  
  $entries = ldap_get_entries($ad, $search);
  $sel = mysql_select_db(&quot;ad&quot;, $con); 
  if (!$sel) 
  { 
  die('Could not select DB: ' . mysql_error()); 
  } 
  
  for ($i=0; $i&lt;$entries[&quot;count&quot;]; $i++) 
  { 
  
  $tag = $entries[$i][&quot;cn&quot;][0]; 
  $so = $entries[$i][&quot;operatingsystem&quot;][0]; 
  $lastl = $entries[$i][&quot;lastlogon&quot;][0];
  
       
          mysql_query(&quot;
          INSERT INTO 
              $table (tag, so, lastlogh)
          VALUES 
              ('$tag', '$so', '$lastl')
              ON DUPLICATE KEY UPDATE         
          tag='$tag',
          so='$so',
          lastlogon='$lastl'
          &quot;);
  
  } 
  mysql_close($con); 
  ldap_unbind($ad);
  if ($entries[&quot;count&quot;] &gt; 0)
  header(&quot;Location: notebooks_list.php&quot;)
  ?&gt;
  </code></pre>
  <p>In this query the last logon timestamp is coded like 130276262860634000</p>
  <p>So I can export everything to excel and decode it, but I found a code that does that, so it saves time.
  I created a new field in the database (lastlogon) and I need to fetch the data from lastlogh field, decode with the new script and store it in the lastlogon field.</p>
  <p>This is the script that I found:</p>
  <pre><code>&lt;?php
  function adConvert ($ad) {
    $seconds_ad = $ad / (10000000);
     //86400 -- seconds in 1 day
     $unix = ((1970-1601) * 365 - 3 + round((1970-1601)/4) ) * 86400;
  
     $timestamp = $seconds_ad - $unix; 
     $normalDate = date(&quot;F d, Y&quot;, $timestamp);
  
        return $normalDate;
  }
  
  //example: echo adConvert($ad);
  ?&gt;
  </code></pre>