简体   繁体   English

将当前日期插入MYSQL表,然后回显

[英]Insert current date to MYSQL table, then echo back

It seems like there are too many complicated ways of doing this, so I'm looking for a clean, succinct answer to this issue. 似乎有太多复杂的方法可以做到这一点,所以我正在寻找一个简洁,简洁的答案。

I write a blog, I click submit, and the title, content, and timestamp INSERTS INTO my blog table. 我写了一个博客,单击提交,然后将标题,内容和时间戳插入到我的博客表中。 Later, the blog is displayed on the blogindex.php page with the date formatted as MM-DD-YYYY. 稍后,博客将显示在blogindex.php页面上,其日期格式为MM-DD-YYYY。

So this is my 3 step question: 这是我的三步问题:

  1. What is the best column type to insert the date into? 插入日期的最佳列类型是什么? (ex: INT, VARCHAR, etc) (例如:INT,VARCHAR等)

  2. What is the best INSERT INTO command to use? 最好使用什么INSERT INTO命令? (ex: NOW(), CURDATE(), etc) (例如:NOW(),CURDATE()等)

  3. When I query the table and retrieve this data in an array, what is the best way to echo it? 当我查询表并在数组中检索此数据时,回显它的最佳方法是什么?

I'm new at PHP/MySQL, so forgive me if I don't know the lingo and am too frustrated reading 1000 differing opinions of this topic that do not address my issue specifically, or only cover one of the 3 questions... 我是PHP / MySQL的新手,所以如果我不懂行话,请原谅我,并且对这个主题有1000多种不同的看法而感到沮丧,这些看法没有具体解决我的问题,或者仅涵盖了三个问题之一...

Here is my opinion on your three questions: 这是我对您的三个问题的看法:

  1. Use the correct data type: Date or DateTime . 使用正确的数据类型: Date或DateTime I would choose for the DateTime type as you store the time as well (might be very handy if you want to have some kind of order, when you added the posts). 在存储时间的同时,我会选择DateTime类型(添加帖子时,如果要进行某种排序,可能会非常方便)。

  2. It all depends whether you just want the Date (use CURDATE() ) or the Date + Time (use NOW() ). 这取决于您是否只想要Date(使用CURDATE() )或Date + Time(使用NOW() )。

  3. You fetch the data and format it how you want it. 您获取数据并按照所需的格式对其进行格式化。 Don't format it yet in the query, just use the correct PHP functions for it (for example with DateTime ). 尚未在查询中格式化它,只需为其使用正确的PHP函数(例如,使用DateTime )。 How you fetch the data, doesn't matter too much; 您如何获取数据并不重要。 you can use PDO or MySQLi or ... 您可以使用PDOMySQLi或...

Always store and process dates and times in UTC and perform timezone adjustments in your presentation layer - it considerably simplifies things in the long-term. 始终使用UTC存储和处理日期和时间,并在表示层中执行时区调整-从长远来看,这大大简化了事情。

MySQL provides a number of different types for working with dates and times, but the only one you need to worry about is DATETIME (the DATE type does not store time information, which messes up time zone conversion as information is lost, and the TIMESTAMP type performs automatic UTC conversion (which can mess up programs if the system time zone information is changed) and has a smaller range (1970-2038). MySQL提供了许多不同的类型来处理日期和时间,但您唯一需要担心的是DATETIMEDATE类型不存储时间信息,因为丢失了信息,这会弄乱时区转换,而TIMESTAMP类型执行自动UTC转换(如果更改了系统时区信息,可能会弄乱程序),并且转换范围较小(1970-2038)。

The CURDATE() function returns only the current date and excludes time information, however this returns information in the local timezone, which can change. CURDATE()函数仅返回当前日期,不包括时间信息,但是这将返回本地时区中的信息,该信息可能会发生变化。 Avoid this. 避免这种情况。 The NOW() function is an improvement, but again, returns data in the current time zone. NOW()函数是一个改进,但同样会返回当前时区的数据。

Because you'll want to keep everything in UTC you'll actually want to use the UTC_TIMESTAMP function . 因为您希望将所有内容都保留在UTC中,所以实际上您将需要使用UTC_TIMESTAMP 函数

To return the value you'll need to execute SQL commands in sequence with variables, like so: 要返回值,您需要按顺序执行SQL命令和变量,如下所示:

SET @now = UTC_TIMESTAMP()
INSERT INTO myTable ( utcDateTimeCreatedOrSomething ) VALUES ( @now )
SELECT @now

Date would probably be the best type, although datetime will work as record more accurate as well. 日期可能是最好的类型,尽管日期时间也可以更准确地记录。

There isn't a 'best insert into', but what do you really want and how accurate you want the date to be. 没有“最好的插入方法”,但是您真正想要的是什么以及您希望日期如何精确。 For a blog, I would say make it datetime and use NOW(). 对于博客,我会说使其成为日期时间并使用NOW()。 so visitors can see quite accurate of when this post is made. 这样访问者可以看到发布此帖子的准确时间。

surely you can easily find huge to run sql and fetch a select query from sql using php by google, so I'll leave this easy work to your self. 当然,您可以轻松地找到运行SQL的巨大功能,并使用google by php通过sql从sql获取选择查询,因此,我将让您轻松完成这项工作。

For echo the date, you can use the php date format such as: 要回显日期,可以使用php日期格式,例如:

$today = date("m-d-y");                         // 03-10-01

I think Styxxy has it pretty well right, but here is a links for your PHP date formatting part... How to format datetime most easily in PHP? 我认为Styxxy非常正确,但是这里是您的PHP日期格式部分的链接... 如何在PHP中最轻松地格式化日期时间? (Supporting link: http://php.net/manual/en/datetime.format.php ) Basically it's (支持链接: http : //php.net/manual/en/datetime.format.php )基本上是
echo date("d/m/Y", strtotime('2009-12-09 13:32:15')) ... although, I think the strtotime is unnecessary as it should already have the type of datetime. echo date(“ d / m / Y”,strtotime('2009-12-09 13:32:15'))...尽管,我认为strtotime是不必要的,因为它应该已经具有datetime类型。

In terms of the MySQL, yes, do it as a datetime col, use NOW() as the SQL keyword, and depending on how you want to get it from the database you could... 就MySQL而言,是的,将其作为日期时间列,将NOW()用作SQL关键字,并取决于您希望如何从数据库中获取它……

SELECT CAST( col_name AS DATE) .... or .... SELECT CAST( col_name AS DATETIME) <-- this last one is implied due to the col type. SELECT CAST( col_name AS DATETIME)....或.... SELECT CAST( col_name AS DATETIME)<-由于col类型而隐含了最后一个。

good luck! 祝好运! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM