來自數據庫的Php Mysql日期和時間 - 剛剛獲得0或1970

[英]Php Mysql Date and Time from Database - just getting 0 or 1970


I'm really struggling and hope someone can please offer some advice.

我真的很掙扎,希望有人可以提供一些建議。

I'm writing a simple web based system comprising of PHP and a Mysql database. I've tried so many ways to achieve what I want without success. All I want to do is simply store a date time in the database alongside each record, and be able to calculate when the record is 30 days old.

我正在編寫一個簡單的基於Web的系統,包括PHP和Mysql數據庫。我已經嘗試了很多方法來實現我想要的但沒有成功。我想要做的只是在每個記錄旁邊的數據庫中存儲一個日期時間,並能夠計算記錄何時為30天。

I don't know if the problem is due to the parameterized mysql binding or something else I'm doing wrong.

我不知道問題是由於參數化的mysql綁定還是其他我做錯了。

Problem 1:

Stored the record date in mysql as DATETIME field. But PHP's strToTime() function cant seem to ever understand the stored datetime - it always either returns 0 or 1970! All I'm storing in the database is from Php's Time() function. If I display the value directly on the page it is indeed a date - so I can't understand why strToTime() fails.

在mysql中將記錄日期存儲為DATETIME字段。但PHP的strToTime()函數似乎無法理解存儲的日期時間 - 它總是返回0或1970!我在數據庫中存儲的所有內容都來自Php的Time()函數。如果我直接在頁面上顯示它確實是一個日期 - 所以我無法理解為什么strToTime()失敗。

Problem 2: After spending hours on the above, I tried changing the mysql db field to an Integer type and storing the Unix Timestamp directly. But then for some reason when retrieving a stored value it will never cast properly as an int in order to perform calculations on it. I tried (int) cast and intval and both can't cast the value back to an int.. So hence Date() functions will not run on the returned value.

問題2:在上面花了幾個小時之后,我嘗試將mysql db字段更改為Integer類型並直接存儲Unix時間戳。但是由於某種原因,在檢索存儲值時,它永遠不會正確地作為int進行轉換,以便對其執行計算。我試過(int)cast和intval並且都不能將值強制轉換為int ..因此Date()函數不會對返回的值運行。

Problem 3: I tried using mysql's own function UNIX_TIMESTAMP() both on DATETIME field types and Int field Types, but even that comes back as a string that doesn't want to cast as an int in order to do calculations on.

問題3:我嘗試在DATETIME字段類型和Int字段類型上使用mysql自己的函數UNIX_TIMESTAMP(),但即使這樣也會作為一個字符串返回,它不想作為int進行轉換以便進行計算。

It makes no sense at all. Clearly I'm doing something wrong but I can't figure out what.

這根本沒有意義。顯然,我做錯了什么,但我無法弄清楚是什么。

//code im using to store the datetime:

//代碼我用來存儲日期時間:

 $mysqltime = time();
    if ($insert_stmt =  $mysqli->prepare("INSERT INTO quotes (quoteID, userID, creation_date) VALUES (?, ?, ?)")) {
        $insert_stmt->bind_param( 'sss', $quoteID, $_SESSION['user_id'], $mysqltime);

//code to demonstrate the problem I'm having retrieving the datetime stored:

//代碼來演示我正在檢索存儲的日期時間的問題:

if ($stmt = $mysqli->prepare("SELECT UID, title, cust, jtitle,     UNIX_TIMESTAMP(creation_date) FROM quotes
   WHERE userID = ? && status = ? 
    ")) {
    $stmt->bind_param('ss', $_SESSION['user_id'], $status );  // Bind to parameters.
    $stmt->execute();    // Execute the prepared query.
    $stmt->store_result();

    // get variables from result.
    $stmt->bind_result( $quoteID, $cust_title, $cust_surname, $job_title, $creation_date);
  //  $stmt->fetch();
 $results =  gmdate("d-m-Y", (int)$creation_date);

1 个解决方案

#1


0  

I personally prefer to use mysql over php for date and time management

我個人更喜歡使用mysql over php進行日期和時間管理

Use an insert statement like this instead:

使用這樣的insert語句:

"INSERT INTO quotes (quoteID, userID, creation_date) VALUES (?, ?, now())"

and a select statement like:

和一個選擇語句,如:

"SELECT UID, title, cust, jtitle, DATE_FORMAT(creation_date,'%d %M %Y')as creation_date".
" FROM quotes WHERE userID = ? && status = ?"

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/06/23/7253dd5bd6e928ab0d6b5f6abc3b3fd8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com