将 PHP 日期字符串作为时间戳保存到 MySQL 数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16457832/
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 11:10:54  来源:igfitidea点击:

Save PHP date string into MySQL database as timestamp

phpmysqldateformattimestamp

提问by nimrod

I am trying to save a datestring in PHP into a MySQL database as timestamp datatype. I found a lot of posts about this, but none of them worked for me. This is what I've tried:

我正在尝试将 PHP 中的日期字符串作为时间戳数据类型保存到 MySQL 数据库中。我找到了很多关于这个的帖子,但没有一个对我有用。这是我尝试过的:

$date =  $_POST['date'];
$timestamp = date("m/d/Y", strtotime($date));
$sql = "insert into sale(service, amount, date, customerid_fk) values('$service', '$amount', '$timestamp', '$id');";

But in the database, I only get:

但在数据库中,我只得到:

0000-00-00 00:00:00

The input string from the POST object is 05/30/2013. Thanks a lot for your support!

来自 POST 对象的输入字符串是05/30/2013. 非常感谢你的支持!

回答by Vijaya Pandey

This might work for you:

这可能对你有用:

$date =  $_POST['date'];
$timestamp = date('Y-m-d H:i:s', strtotime($date));  

 $sql = "insert into sale(service, amount, date, customerid_fk) values('$service', '$amount', '$timestamp', '$id');";

回答by Sam Deering

This worked for me:

这对我有用:

//timestamp fix
$date = new \DateTime();
$created_at = $date->setTimestamp(intval($timestamp));

回答by swaroop suthar

SELECT UNIX_TIMESTAMP('2005-03-27 03:00:00');  -- **1111885200**
SELECT FROM_UNIXTIME(1111885200);              -- **2005-03-27 03:00:00**
$date =  $_POST['date'];
$timestamp = date('Y-m-d H:i:s', strtotime($date));  

$sql = "insert into sale(service, amount, date, customerid_fk) values
    ('$service', '$amount', UNIX_TIMESTAMP('$timestamp'), '$id');"

回答by Marco Valencia

you should use mysql methods

你应该使用 mysql 方法

I got this date from a datebox of easyui

我从 easyui 的 datebox 中得到了这个日期

08/25/2016

08/25/2016

then

然后

$date = "FROM_UNIXTIME(".strtotime(08/25/2016).")";

INSERT INTO TABLE (col1, col2, dateI) values($a, $X, $date);

回答by JAYESH

We have NOW()function in MySQL.

我们NOW()MySQL 中有函数。

insert into sale(service, amount, date, customerid_fk)values('$service', '$amount', NOW(), '$id');

回答by Emanuele Pavanello

you need to remove apex for put timestamp as long

你需要删除顶点放置时间戳

insert into sale(service, amount, date, customerid_fk) values('$service', '$amount', $timestamp, '$id')

or use this format for date 'Y-m-d H:i:s'

或将此格式用于日期 'Ymd H:i:s'

回答by user2362083

if you have date field type as DATEor DATE/TIMEit will add timestamps like this. you need to make your date field type as varchar and add your timestamps as strings.

如果您的日期字段类型为 asDATEDATE/TIME它会添加这样的时间戳。您需要将日期字段类型设为 varchar 并将时间戳添加为字符串。

if you need to add date in format d/m/y then mysql has different format like y-m-dso before adding you can do like this

如果您需要以 d/m/y 格式添加日期,y-m-d那么在添加之前,mysql 具有不同的格式,您可以这样做

date('y-m-d', strtotime('your-date-string'))

date('y-m-d', strtotime('your-date-string'))