使用PHP& amp;记录日期作为时间戳SQL

时间:2013-04-10 09:10:34

标签: php timestamp unix-timestamp

第一次在这里发帖提问,请耐心等待。以下是我正在制作的网页代码:

<?php require_once('Connections/reps.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  mysql_select_db($database_reps, $reps);
  $query_bonusset = "SELECT bonus FROM staff WHERE id = '".$_POST['staffMember']."'";
  $bonusset = mysql_query($query_bonusset, $reps) or die(mysql_error());
  $row_bonusset = mysql_fetch_assoc($bonusset);
  $totalRows_bonusset = mysql_num_rows($bonusset);


  mysql_select_db($database_reps, $reps);
  $query_listprice = "SELECT Price FROM products WHERE id = '".$_POST['product']."'";
  $listprice = mysql_query($query_listprice, $reps) or die(mysql_error());
  $row_listprice = mysql_fetch_assoc($listprice);
  $totalRows_listprice = mysql_num_rows($listprice);

  $insertSQL = sprintf("INSERT INTO orders (ID, customerName, staffMember, SalesPrice, Price, AgeOfPayment, product, Quantity, orderDate, bonus) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['ID'], "int"),
                       GetSQLValueString($_POST['customerName'], "text"),
                       GetSQLValueString($_POST['staffMember'], "text"),
                       GetSQLValueString($_POST['SalesPrice'], "int"),
                       GetSQLValueString($row_listprice['Price'], "int"),
                       GetSQLValueString($_POST['AgeOfPayment'], "int"),
                       GetSQLValueString($_POST['product'], "text"),
                       GetSQLValueString($_POST['Quantity'], "int"),
                       GetSQLValueString($_POST['orderDate'], "int"),
                       GetSQLValueString($row_bonusset['bonus'], "int"));

  mysql_select_db($database_reps, $reps);
  $Result1 = mysql_query($insertSQL, $reps) or die(mysql_error());

  $insertGoTo = "vieworders.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_reps, $reps);
$query_customers = "SELECT * FROM customers WHERE Status = 'Active'";
$customers = mysql_query($query_customers, $reps) or die(mysql_error());
$row_customers = mysql_fetch_assoc($customers);
$totalRows_customers = mysql_num_rows($customers);

mysql_select_db($database_reps, $reps);
$query_products = "SELECT * FROM products";
$products = mysql_query($query_products, $reps) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);

mysql_select_db($database_reps, $reps);
$query_staff = "SELECT * FROM staff";
$staff = mysql_query($query_staff, $reps) or die(mysql_error());
$row_staff = mysql_fetch_assoc($staff);
$totalRows_staff = mysql_num_rows($staff);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Site Title</title>
<?php


require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");

?>

</head>

<body>

<div id='wrapper'>
<div id='top'><div id='logo'></div></div>
<div id='content'>
<h1>Site Name</h1>
<h2>Change This</h2>
<div id='left-nav'>

<?php include("left-nav.php"); ?>

</div>
<div id='main'>


<div id ="prodtable">
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Customer Name:</td>
      <td><select name="customerName">
        <?php 
do {  
?>
        <option value="<?php echo $row_customers['ID']?>" ><?php echo $row_customers['Name']?></option>
        <?php
} while ($row_customers = mysql_fetch_assoc($customers));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Sold by: </td>
      <td><select name="staffMember">
        <?php 
do {  
?>
        <option value="<?php echo $row_staff['ID']?>" ><?php echo $row_staff['StaffName']?></option>
        <?php
} while ($row_staff = mysql_fetch_assoc($staff));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Sales Price (€): </td>
      <td><input type="text" name="SalesPrice" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Product:</td>
      <td><select name="product">
        <?php 
do {  
?>
        <option value="<?php echo $row_products['ID']?>" ><?php echo $row_products['ProductName']?></option>
        <?php
} while ($row_products = mysql_fetch_assoc($products));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Quantity:</td>
      <td><input type="text" name="Quantity" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Order Date:</td>
      <td><input type="text" name="orderDate" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="ID" value="" />
  <!-- Removed list price as it was declared above -->
  <input type="hidden" name="AgeOfPayment" value="" />
  <!-- Removed bonus as it was decalred above -->
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</div>
<p>&nbsp;</p>
</div>
<div id='bottom'></div>
</div>


</body>
</html><?php
mysql_free_result($customers);

mysql_free_result($products);

mysql_free_result($staff);
?>

我希望做的是,一旦我提交表单,它将采用输入的日期并将其转换为SQL数据库上的Unix时间戳。

任何正确方向的帮助或刺激都会很棒。

提前致谢

3 个答案:

答案 0 :(得分:1)

  1. 无法从任意格式转换数据。没有魔力。即使有人告诉你有。所以,首先你必须先确定日期格式。
  2. 将日期转换为unix时间戳时,没有意义。日期的时间戳将是模糊的并且难以使用。为什么打扰呢?为什么不使用DATE字段类型来存储日期。听起来很明智,是吗?
  3. 您真正需要的是验证输入的日期并将其转换为2013-04-10格式然后插入。

    我的建议。

    • 使用您在Google上可以找到的任何日期选择器来填写表单。
    • 使用explode() + checkdate()strtotime()验证输入的值。
    • 将值作为字符串插入DATE类型的字段

    顺便说一句,有一种更好的方法来构建查询,没有GetSQLValueString函数。

    首先,您可以使用sprintf本身的强大功能:

    paraQuery("INSERT INTO orders VALUES (%d,%s,%s,%d,%d,%d,%s,%d,%d,%d",
               $_POST['ID'], 
               $_POST['customerName'], 
               $_POST['staffMember'],
               $_POST['SalesPrice'], 
               $row_listprice['Price'], 
               $_POST['AgeOfPayment'], 
               $_POST['product'], 
               $_POST['Quantity'],
               $_POST['orderDate'], 
               $row_bonusset['bonus']
               );
    

    (paraQuery是this answer

    中的函数

    接下来,对于长插入,最好使用辅助函数而不是手动编写它们:

    $fields = explode(",","ID,customerName,staffMember,SalesPrice,Price,AgeOfPayment,product,Quantity,orderDate,bonus");
    $query  = "INSERT INTO orders SET ".dbSet($fields);
    

    (dbSet是来自this question的函数)

答案 1 :(得分:0)

我认为strtotime()可以帮到你:

http://php.net/manual/en/function.strtotime.php

答案 2 :(得分:-1)

使用date_parse_from_format()将您的发布日期转换为数组... See php.net

然后使用mktime()生成unix时间戳 See php.net