在我的 PHP 应用程序中,当我使用 PHP 的 date()
命令格式化日期时,我从 MySQL 数据库获取日期。我想整理一下并将其完全移至视图,而不是检索。但我遇到了一些问题。
从数据库获取日期时,我在对其运行 strtotime()
后将其传递给应用程序的其余部分,以便 date()
函数拥有它们可以使用的东西与. 一起工作。这是个好主意吗?或者我错过了一些更好和明显的东西?我正在处理的日期几乎都是 DATE 类型,而不是 DATETIME 类型。
这样,我就得到了日期,并且可以使用 date()
将其显示在网页上。但有时,我会从数据库中获取一些内容,然后将其插入数据库中的其他位置,并且必须在后端中寻找需要 date()
的位置,这违背了将其分离出来的目的到视图。我的检索函数有助于返回 strtotime($date_from_db)
,然后我在另一个表中创建新条目...并且所有新条目都必须通过验证和准备函数。问题就在这里。我通过查看 strtotime()
是否为 0000-00-00 以外的值来验证和准备日期。但我无法在时间戳上运行 strtotime()
;在我运行的测试中,如果给了我 12/31/1969。所以我的问题是:
- 是否可以以某种方式在时间戳上运行
strtotime()
?
- 如果不是,是否有一种简单的方法可以知道变量是否是时间戳?所以我可以简单地运行
date()
而不是处理 strtotime()
吗?除了看看它是否会在 1969 年 12 月 31 日发布之外,因为我不知道我是否可以相信失败会保持一致。除非那是一件有效的事情?
- 我这样做完全错误吗?
In my PHP app, I get dates from the MySQL database, when I format them with PHP's date()
command. I want to tidy that up and move it all entirely to the view, rather than the retrieval. But I ran into some problems.
When getting a date from the database, I pass it to the rest of the app after running strtotime()
on it, so that the date()
functions have something they can work with. Is this a good idea? or am I missing something better and obvious? The dates that I'm dealing with are nearly all in the DATE
type, not DATETIME
.
So, I get the date, and can use date()
to display it on the webpage. But sometimes, I'll get something from the database solely to insert elsewhere in the database, and having to hunt through the backend to find places that need date()
kind of defeats the purpose of separating it out to the view. My retrieval functions helpfully return strtotime($date_from_db)
, and then I create the new entry into the other table... and all new entries must pass through the validation and preparation functions. And herein lies the problem. I validate and prepare dates by seeing if they strtotime()
to something other than 0000-00-00. But I can't run strtotime()
on a timestamp; in the test I ran, if gave me 12/31/1969. So my questions:
- Is it possible somehow to run
strtotime()
on a timestamp?
- If not, is there an easy way to know if a variable is a timestamp? So I could simply run
date()
on it rather than dealing with strtotime()
? Apart from seeing if it comes out to 12/31/1969, because I don't know if I can trust a failure to be consistent. Unless that's a valid thing to do anyway?
- And am I doing this completely wrong?
发布评论
评论(4)
作为一般方法,我很想:
使用 MySQL 的日期时间类型存储日期/时间值,因为这将允许您有意义地查询 MySQL 中的数据,而不必求助于使用 >= "timestampvalueX"等。
保留所有 "在 PHP 脚本中使用时间戳格式的“实时”日期信息。
因此,您可能希望以该格式存储所有与日期/时间相关的信息作为目标。您可以轻松地将其转换为 PHP 可以在查询本身中使用的格式(使用 UNIX_TIMESTAMP) 或通过分解 YYYY-MM-DD HH:MM:SS 格式并使用 PHP 的 mktime 创建“标准”时间戳。 (尽管您可能希望使用 checkdate 进行验证。
)针对您的具体问题:
如果通过时间戳,strotime 将失败。
作为临时措施,您可以编写一个函数来检查提供的值是否为 10 位整数,如果是,则假定它是本机时间戳。 (
if(is_int($value) && strlen($value) == 10)...
)听上去有点混乱,所以作为一个合理的短期目标,您可能应该尝试标准化 MySQL 和 PHP 中的使用。
As a general approach, I'd be very tempted to :
Store date/time values using MySQL's datetime type, as this will allow you to meaningfully query the data within MySQL without having resort to either using >= "timestampvalueX", etc.
Keep all "live" date information using within PHP scripts in timestamp format.
As such, you probably want to aim to store all of the date/time related information in that format as a goal. You can the trivially convert this into a format PHP can use either in the query itself (using UNIX_TIMESTAMP) or by exploding the YYYY-MM-DD HH:MM:SS format and using PHP's mktime to create a "standard" timestamp. (Although you might want to use checkdate for validation purposes.)
Getting back to your specific questions:
strotime will fail if a timestamp is passed.
As an interim measure you could write a function that checked if the provided value was a 10 digit integer, and presume that it was a native timestamp if it was. (
if(is_int($value) && strlen($value) == 10)...
)It's a bit of a mess by the sounds of things, so as a reasonably short term goal you should probably try and standardise usage both in MySQL and PHP.
当你查询时间戳时让mySQL来处理它。
http://dev.mysql .com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
该查询将检索您需要的格式化时间戳,从而使您无需对字符串进行任何处理。
Let mySQL handle it when you query the timestamp.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
The query will retrieve the timestamp formatted as you need it saving you from having to do any processing the string.
12/31/1969 是 UNIX 纪元 1-1-1970 的开始,带有时区偏移。
对时间戳运行
strtotime()
将返回 0,当您通过date()
运行它时,最终结果为 1-1-1970 UTC。检查strtotime
结果是否为 0 就可以了。12/31/1969 is the start of the UNIX epoch, 1-1-1970, with a timezone offset.
Running
strtotime()
on a timestamp will return 0, which winds up as 1-1-1970 UTC when you run it throughdate()
. Checking for astrtotime
result of 0 should do it.简而言之,在从数据库返回的时间戳前面添加一个“@”符号,您就可以像普通时间戳一样使用它。建议将此解决方案作为一种尽可能减少与提问者熟悉的技术和方法的分歧的方法(以最小的努力/混乱)。
您的数据库返回时间戳的一个版本,它是一个字符串,而 PHP 时间戳是一个时间戳对象。
使用“@”技巧,您可以一致地编码。无论您是处理 PHP 中的时间戳对象还是 MYSQL 返回的字符串,这并不重要。
应从时间戳字段检索来自数据库的时间戳,如下所示:
In short, prepend an '@' symbol to your timestamp returned from your database and you can use it in the same way as normal timestamp. This solution is suggested as a means of diverging as minimally as possible from the techniques and approaches which the questioner is familiar with (for minimal effort/confusion).
your db returns a version of a timestamp which is a string, whereas a PHP timestamp is a timestamp object.
with the '@' trick, you can code consistently. it doesn't matter whether you're processing a timestamp object from PHP or a string returned by MYSQL.
the timestamp from the DB should be retrieved from the timestamp field thus: