在php中格式化mysql时间戳,有几个条件
我试图根据以下条件在 PHP 中格式化 SQL 时间戳,但不知道如何实现。有人能指出我正确的方向吗?
- 如果时间戳为 TODAY,则显示为 4:15PM 或 12:30AM
- 如果时间戳在 TODAY 之前但在过去 7 天内,则列出为“Sunday”或“Monday”
- 如果时间戳在 7 天前之前,则列出为“mm” /dd/yy'
我该怎么做?
I'm trying to format a SQL timestamp in PHP based on the following conditions, but can't figure out how. Can anyone point me in the right direction?
- If the timestamp was TODAY, display as 4:15PM or 12:30AM
- If the timestamp was before TODAY but in the past 7 DAYS, list as 'Sunday' or 'Monday'
- If the timestamp was before 7 DAYS ago, list as 'mm/dd/yy'
How would I go about that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您需要将 MySQL 时间转换为 unix 时间戳,这是大多数 php 日期函数使用的。如果您使用 MySQL 的 DateTime 类型,则可以使用 MySQL 函数 unix_timestamp() mysql 日期函数。或者您可以使用 strtotime($mysqlDateTime) 函数 将 mysql 日期转换为 PHP 中的 unix 时间戳php strtotime 函数
一旦您获得了要格式化的时间的 unix 时间戳,转换将如下所示(86400 是 24 小时内的秒数):
此方法的优点是不需要额外的对象在 PHP 中创建(有点慢)或在 SQL 服务器上执行不必要的计算。了解 MySQL 的时间戳类型将数据存储为 unix 时间戳(自 1970 年 1 月 1 日以来的秒数)值可能也有所帮助,与使用 64 位存储的日期时间相比,仅需要 32 位存储。 32 位应该对每个人来说都足够了,直到 2038 年或者什么时候......
First you need to convert the MySQL time to a unix timestamp which is what most of php date functions use. If you are using MySQLs DateTime type, you can perform the conversion in SQL with the MySQL function unix_timestamp() mysql date functions. Or you can convert the mysql date to a unix timestamp in PHP with the strtotime($mysqlDateTime) function php strtotime function
once you have the unix timestamp of the time you would like to format, the conversion would look something like this (86400 is number of seconds in 24 hours):
This method has the benefit of not requiring extra object creation in PHP (a tad slow) or performing unnecessary calculations on the SQL server. It might also help to know that MySQL's timestamp type stores data as a unix timestamp (number of seconds since Jan 1 1970) value requiring only 32bits for storage compared to datetime which uses 64bits of storage. 32 bits should be enough for everyone, until 2038 or something....
您可以通过 PHP 的 diff() 或 msql datediff() 检查日期差异
http ://www.php.net/manual/en/datetime.diff.php
然后检查差异是否为零或等于1或大于7
http://php.net/manual/en/function.date.php
这应该有效。希望如此:-)
you can check date difference by by diff() of PHP or by msql datediff()
http://www.php.net/manual/en/datetime.diff.php
Then check difference is zero or equal to 1 or greater than 7
http://php.net/manual/en/function.date.php
This should work. Hope so :-)
你只需要使用一个条件:
这还没有经过测试,你需要将你的 mySql 日期放入 php DateTime 对象中,但它应该让你非常接近。
You just have to use a conditional:
This hasn't been tested and you'll need to get your mySql date into a php DateTime object but it should get you pretty close.
我假设你正在谈论 MySQL
TIMESTAMP
数据类型,因为我不认为 MySQL 实际上有一个像 Unix 时间戳这样的数据类型(即自纪元以来的秒数),因此您必须首先使用strtotime
函数:这将返回一个 Unix 时间戳 你可以使用。
接下来,我们将定义更多时间戳来与该值进行比较:
首先,我们想知道今天早上午夜的时间戳。为此,您需要将字符串“today”传递给
strtotime
:接下来,我们需要知道 7 天前的时间戳。您必须在
“1 周前”
和“1 周前午夜”
之间进行选择。两者之间的区别在于,midnight
将返回当天上午 12 点的时间戳,而没有它的版本将返回 7 天前的当前时间(例如今天、区别在于midnight
将在 4 月 7 日上午 12 点返回,而非午夜版本将在 4 月 7 日下午 3:45 返回):(注意,有 许多
strtotime
能够理解的格式,包括许多 相对格式,例如上面使用的“今天”和“1 周前”示例。)接下来,我们需要定义日期格式 在每种情况下使用:
最后,我们只需进行比较,并使用
date
函数:这将为您留下一个名为
$date
的字符串变量,其中包含数据库提供的适当格式的时间戳,您可以根据需要显示在您的页面上。I assume you're talking about the MySQL
TIMESTAMP
datatype, since I don't think MySQL actually has a datatype like a Unix timestamp (i.e. seconds since epoch), so you'll have to first convert the date you get using thestrtotime
function:This will return a Unix timestamp you can play with.
Next we'll define a couple more timestamps to compare this value against:
First, we want to know the timestamp for midnight this morning. For that, you'll pass the string "today" to
strtotime
:Next, we need to know the timestamp for seven days ago. You'll have to choose between
"1 week ago"
and"1 week ago midnight"
. The difference between these two is thatmidnight
will return the timestamp for 12am on that day, while the version without it will return the current time, seven days ago (e.g. today, the difference would be thatmidnight
will return 12 AM on April 7 and the non-midnight version would, right now, return 3:45PM on April 7):(Note, there are many formats that
strtotime
understands, including many relative formats like the "today" and "1 week ago" examples used above.)Next, we need to define the date formats to use in each case:
Finally, we just do our comparisons, and format our timestamp using the
date
function:This will leave you with a string variable called
$date
which contains your database-provided timestamp in the appropriate format, which you can display on your page as needed.