php 获取当前时间/日期的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15533060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:25:32  来源:igfitidea点击:

Timestamp to get current time/date

phpdatetimetimestamp

提问by user2192221

I need simple code using PHP to get the current date/time using TIMESTAMP and insert this into a database.

我需要使用 PHP 的简单代码来使用 TIMESTAMP 获取当前日期/时间并将其插入到数据库中。

I have a field called "ArrivalTime" within a database as TIMESTAMP.

我在数据库中有一个名为“ArrivalTime”的字段作为 TIMESTAMP。

EDIT:

编辑:

<?php
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("a&e", $con) or die('Could not select database.');

// validate

// 证实

$time = time(); $date = date('Y-m-d H:i:s',$time);

$sql="INSERT INTO Patient(Forename, Surname, Gender, Date_Of_Birth, Address,    Patient_History, Illness, Priority, Arrival_Time)
VALUES('$patient_name', '$patient_lastname', '$gender', '$date', '$address', '$history', '$illness', '$priority', '$time')";
mysql_query($sql,$con) or die('Error: ' . mysql_error());

echo "1 record added";
// close connection 
mysql_close($con);
?>

Many thanks

非常感谢

回答by Alex

main.php

主文件

<?php
    require('connect.php');

    $time = time();


    $sql = "INSERT INTO yourtablename (ArrivalTime) Values ('$time')";
    mysql_query($sql);

    ?>

P.S: in the sql statement i'm sure you'll need to put other things in the other fields ,so you just replace the one above by this:

PS:在sql语句中,我确定您需要将其他内容放在其他字段中,因此您只需将上面的内容替换为:

$sql = "INSERT INTO yourtablename (field1, field2, ArrivalTime) Values ('$value1','$value2','$time')";

connect.php

连接.php

<?php

$error = "Couldn't connect";
$connect = mysql_connect("localhost","username","password") or die($error);
mysql_select_db("yourdatabase") or die($error);

?>

回答by artahian

The query will be:

查询将是:

 INSERT INTO mytable(ArrivalTime) VALUES(UNIX_TIMESTAMP())

回答by Aris

mysql has a function UNIX_TIMESTAMP()for getting a Unix timestamp as an unsigned integer.

mysql 有一个函数UNIX_TIMESTAMP()用于获取 Unix 时间戳作为无符号整数。

Example:

例子:

mysql> SELECT UNIX_TIMESTAMP(); -> 1196440210

mysql> SELECT UNIX_TIMESTAMP(); -> 1196440210

So you can use this sql query:

所以你可以使用这个 sql 查询:

insert into tableName(ArrivalTime) values(UNIX_TIMESTAMP())

插入 tableName(ArrivalTime) 值(UNIX_TIMESTAMP())