茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1674225 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00343.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2016/2/18 13:03:55
Converting string to MySQL timestamp format in php

I am writing a query in php using a string sent from a android java application.
The query is something like :
$insertSQL = sprintf("INSERT INTO app_DuckTag (taste) VALUES (%s) WHERE species=%s AND timestamp=%s",
                         GetSQLValueString($_POST['taste'], "text"),
                         GetSQLValueString($_POST['species'], "text"),
                         GetSQLValueString($_POST['timestamp'], "text"));

But I doubt timestamp is stored as a string inside MySQL.
How should I convert the string to time format as in MySQL?
The strtotime(string) php function converts it to unix time.
But the MySQL stores it differently, I guess. Thank you.

EDIT: This is how it shows up in MySQL phpmyadmin: 2011-08-16 17:10:45

EDIT: My query is wrong though. Cannon use a where clause with Insert into.
The query has to be UPDATE .... SET ... = ... WHERE ....
But the accepted answer is the correct way to use the time inside the WHERE clause.
php android mysql string timestamp
shareimprove this question



This should be all:
date("Y-m-d H:i:s", strtotime($_POST['timestamp']));

if you want to check for timezones and such you should use strftime instead of date
shareimprove this answer



If you can get the input 'string' (which you haven't provided the format that you're receiving it in) to a Unix timestamp value, you can easily convert it to a MySQL datetime format like this:
$mysqldatetime = date("Y-m-d H:i:s", $unixTimeStampValue);

This will produce a string similar to the following:
    2011-08-18 16:31:32

Which is the format of the MySQL datetime format. If you are using a different time format, then the first argument to the date function will be different.

See the manual for the date function for more information and other ways you can format the value that it returns.

Edit

You are receiving a string formatted in MySQL datetime format. After sanitizing it, you can insert it directly into the database. To MySQL it may be a 'datetime' data type, but to PHP it is simply a string (just like your entire SQL query is nothing more than a string to PHP).
$timestamp = mysql_real_escape_string($_POST['timestamp']);

You should be able to safely insert that into your database for the timestamp field.


原文出處:
android - Converting string to MySQL timestamp format in php - Stack Overflow
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|