What's the recommended way to store current time using PHP and MySQL?

前端 未结 5 1459
时光说笑
时光说笑 2020-12-31 10:55

My initial approach was:

$current = time(); // save this to column CURRENT_TIME with column type VARCHAR

//retrieve it like this
$retrieved = mysql_query(..         


        
5条回答
  •  臣服心动
    2020-12-31 11:32

    It is recommended to use mysql timestamp (YYYY-MM-DD HH:MM:SS) field type to store time and date variables in mysql.

    $sDate = date("Y-m-d H:i:s"); // 2015-04-07 07:12:51
    mysql_query("insert into `table_name` set `created_on` = '$sDate'");
    

    It gives you ability to use mysql functions to compare dates, calculate time differences and so, directly in your mysql query.

    Also you can always retrieve the timestamp using strtotime() function.

    $result = mysql_query("select `created_on` from `table_name`");
    $row = mysql_fetch_row($result);
    $iTimestamp = strtotime($row[0]); // 1428390771
    

提交回复
热议问题