【问题标题】:How to insert same `datetime` variable into different tables?如何将相同的“日期时间”变量插入不同的表中?
【发布时间】:2017-08-16 08:05:19
【问题描述】:

我想使用两个函数将datetime 变量存储到不同的表中。我在CI 中使用了constraint,但仍然没有运气。

这是约束:

$date_now = date("ymdhis");
define('TODAY_DATE',$date_now);

这些是函数:

public function save_activity_m(){
   foreach($details as $rows){          

      $stock_in          = $rows['product']."_".TODAY_DATE;
      $data['STOCK_IN']  = ($rows['product'] == "") ? NULL : $stock_in;

      $this->MProduct->ins_product_m($data);
   }
   echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>";
}

public function save_notifikasi(){

    $lampiran = $this->input->post('lamp');     

    $data['note_date'] = $lampiran."_".TODAY_DATE;      
    $data['note']       = $this->input->post('isi');

    $this->MProduct->ins_notif($data);
     echo "<script type='text/javascript'>alert('You have a notification');</script>";


}

如何使$data['STOCK_IN']$data['note_date']的日期时间相同?

【问题讨论】:

  • 您使用什么数据库服务器和数据类型?对于 MySQL 5.7 或 MariaDB 之类的东西,您需要使用 DATETIME 字段,日期格式为 Y-m-d H:i:s
  • 您可以回显常量变量以检查它是否具有相同的值。
  • 我将日期时间与表格中的文本结合起来。所以我使用 varchar
  • 是的 - 抱歉,扫描问题时错过了:|
  • 所以php中的日期时间必须相同

标签: php codeigniter date datetime


【解决方案1】:

由于网络是无状态的,PHP 变量中的数据不会从一个页面(或加载)保存到另一个页面;本质上,您每次都是从头开始启动应用程序。

解决此问题的唯一方法是使用某种半持久存储,例如 cookie 或会话变量(或数据库等持久存储) - 设置 constant,例如define('TODAY_DATE',$date_now); 只会在当前脚本执行时使该数据保持不变。

这是一个使用会话存储的基本示例 ($_SESSION):

<?php

// crank up the session
// you may well have one running already, 
// in which case ignore this
session_start();

// store the execution time for this script
// as a session variable if it's NOT already set
// i.e. don't overwrite it
if(empty($_SESSION['time_now'])) $_SESSION['time_now'] = date("ymdhis");


public function save_activity_m() {
    foreach($details as $rows) {          
        $stock_in = $rows['product'] . "_" . $_SESSION['time_now'];
        $data['STOCK_IN'] = ($rows['product'] == "") ? NULL : $stock_in;
        $this->MProduct->ins_product_m($data);
    }

    echo "<script type='text/javascript'>alert('New stock arrived');window.top.location.reload();</script>";
}

/**
 * Assuming this is the last thing you want to
 * do with 'time_now' you should unset it here
 */
public function save_notifikasi() {
    $lampiran = $this->input->post('lamp');     
    $data['note_date'] = $lampiran . "_" . $_SESSION['time_now'];
    $data['note'] = $this->input->post('isi');
    $this->MProduct->ins_notif($data);

    // since we're done with the 'time_now' session
    // variable we need to unset it...
    unset($_SESSION['time_now']);

    echo "<script type='text/javascript'>alert('You have a notification');</script>";
}

// just to be on the safe side unset the 'time_now' session var 
// if it's older than 1 minute - otherwise future calls to this 
// script, by the same user, during the same session will use 
// the stored value from $_SESSION['time_now']
if(isset($_SESSION['time_now'])) {
    $sessionTime = DateTime::createFromFormat('ymdhis', $_SESSION['time_now']);
    $oneMinuteAgoTime = new DateTime('-1 minute');

    if($sessionTime < $oneMinuteAgoTime) {
        unset($_SESSION['time_now']);
    }
}

需要注意的是,因为您已将时间存储在会话变量中,除非您更新或取消设置它,否则它将始终存在(对于当前会话) - 因此,如果用户再次运行脚本,它将只是使用会话中存储的时间。

我已经拨打了几个unset() 电话来尝试解决这个问题。

【讨论】:

    【解决方案2】:

    PHP: define。它是一个常量,如果两个函数在脚本运行的同时执行,它应该具有相同的值。

    【讨论】:

    • 我用过。并且以秒为单位。
    • 可能是数据库的配置。该行可能有 DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP。也许?
    • 日期时间的默认格式为:YYYY-MM-DD HH:MM:SS
    • 对不起,我的意思是 varchar。
    • 这样做:$date_now = date("Y-m-d H:i:s");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多