2017-07-26 117 views
0

我有這樣的語句來保存在MySQL的日期,但我會把它保存爲一個例子:格式日期在MySQL

September 24, 2011, 6:39 am

或者我應該擁有它並且當我檢索它時,我會如此迴應它嗎?

try { 
     $date = date('Y-m-d H:i:s'); 
     //insert into database with a prepared statement 
     $stmt = $db->prepare('INSERT INTO admin (username, password, email, register_date, active) VALUES (:username, :password, :email, :register_date, :active)'); 
     $stmt->execute(array(
      ':username' => $_POST['username'], 
      ':password' => $hashedpassword, 
      ':email' => $_POST['email'], 
      ':register_date' => $date, 
      ':active' => $activasion 
    )); 
//else catch the exception and show the error. 
     } catch(PDOException $e) { 
      $error[] = $e->getMessage(); 
     } 
+0

備註:請不要使用'>'字符作爲個人格式設置;他們是爲了一個特殊的目的。我[編輯](https://stackoverflow.com/revisions/45334837/2)那些了。 –

+0

這是我如何從表 $ nestedData [] = $ row [「register_date」]; –

回答

0
<?php 
/* 
    * F --> for full month 
    * D -->day 
    * Y --> Year 
    * H -->Hour 
    * m -->minute 
    * A -- PM or AM 

*/ 
$date = date("F d, Y, H:m A"); 
echo $date; 
?> 

輸出將被

July 26, 2017, 21:07 PM 
+0

while($ row = mysqli_fetch_array($ query)){//準備一個數組 \t $ nestedData = array(); \t $ register_date = $ row [「register_date」]; \t $ register_date = date(「F d,Y,H:m A」); \t $ nestedData [] = $ row [「adminID」]; \t $ nestedData [] = $ row [「username」]; \t $ nestedData [] = $ row [「email」]; \t $ nestedData [] = $ register_date; \t $ nestedData [] = $ row [「login_date」]; \t $ nestedData [] = $ row [「active」]; \t \t $ data [] = $ nestedData; }它只是爲所有記錄 –

+0

打印相同的日期,因爲您將'date'保存在數據庫中的列類型'date'中,所以應該將'date'保存爲列類型'varchar(50)' –

0
// original datetime string: 
$str = 'September 24, 2011, 6:39 am'; 

$stmt->execute(array(
     ':username' => $_POST['username'], 
     ':password' => $hashedpassword, 
     ':email' => $_POST['email'], 
     ':register_date' => strftime("%Y-%m-%d %H:%M:%S", strtotime($str)), 
     ':active' => $activasion 
)); 

日期將被格式化爲:2011-09-24 06:39:00和MySQL會很高興。

Live demo

文檔:strftime | strtotime