如何将相同的`datetime`变量插入到不同的表中?

时间:2017-08-16 08:05:20

标签: php codeigniter date datetime

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

这是约束:

$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']的日期时间相同?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

由于Web是无状态的,因此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()次调用,试图解决这个问题。