在php中格式化mysql时间戳,有几个条件

发布于 2024-11-01 21:57:15 字数 224 浏览 4 评论 0原文

我试图根据以下条件在 PHP 中格式化 SQL 时间戳,但不知道如何实现。有人能指出我正确的方向吗?

  1. 如果时间戳为 TODAY,则显示为 4:15PM 或 12:30AM
  2. 如果时间戳在 TODAY 之前但在过去 7 天内,则列出为“Sunday”或“Monday”
  3. 如果时间戳在 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?

  1. If the timestamp was TODAY, display as 4:15PM or 12:30AM
  2. If the timestamp was before TODAY but in the past 7 DAYS, list as 'Sunday' or 'Monday'
  3. If the timestamp was before 7 DAYS ago, list as 'mm/dd/yy'

How would I go about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-11-08 21:57:15

首先,您需要将 MySQL 时间转换为 unix 时间戳,这是大多数 php 日期函数使用的。如果您使用 MySQL 的 DateTime 类型,则可以使用 MySQL 函数 unix_timestamp() mysql 日期函数。或者您可以使用 strtotime($mysqlDateTime) 函数 将 mysql 日期转换为 PHP 中的 unix 时间戳php strtotime 函数

一旦您获得了要格式化的时间的 unix 时间戳,转换将如下所示(86400 是 24 小时内的秒数):

function displayDate($timestamp)
{
    $secAgo = time() - $timestamp;
    // 1 day
    if ($secAgo < 86400)
        return date('h:i:A', $timestamp);
    // 1 week
    if ($secAgo < (86400 * 7))
       return date('l', $timestamp);
    // older than 1 week
    return date('m/t/y', $timestamp);
}

此方法的优点是不需要额外的对象在 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):

function displayDate($timestamp)
{
    $secAgo = time() - $timestamp;
    // 1 day
    if ($secAgo < 86400)
        return date('h:i:A', $timestamp);
    // 1 week
    if ($secAgo < (86400 * 7))
       return date('l', $timestamp);
    // older than 1 week
    return date('m/t/y', $timestamp);
}

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....

旧竹 2024-11-08 21:57:15

您可以通过 PHP 的 diff() 或 msql datediff() 检查日期差异

http ://www.php.net/manual/en/datetime.diff.php

然后检查差异是否为零或等于1或大于7

h 12-hour format of an hour with leading zeros 01 through 12 date('H:i:s')
i Minutes with leading zeros 00 to 59 
s Seconds, with leading zeros 00 through 59 

G 24-hour format of an hour without leading zeros 0 through 23 

USE DATE(G) to find AM or PM

if($TODAY)
date('h:i:s')PM
ELSE IF ($THISWEEK)
l (lowercase 'L') A
 full textual representation of the day of the week Sunday through Saturday 
ELSE IF($BEFOREONEWEEK)
date('d-m-y')

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

h 12-hour format of an hour with leading zeros 01 through 12 date('H:i:s')
i Minutes with leading zeros 00 to 59 
s Seconds, with leading zeros 00 through 59 

G 24-hour format of an hour without leading zeros 0 through 23 

USE DATE(G) to find AM or PM

if($TODAY)
date('h:i:s')PM
ELSE IF ($THISWEEK)
l (lowercase 'L') A
 full textual representation of the day of the week Sunday through Saturday 
ELSE IF($BEFOREONEWEEK)
date('d-m-y')

http://php.net/manual/en/function.date.php

This should work. Hope so :-)

沫离伤花 2024-11-08 21:57:15

你只需要使用一个条件:

$now = new DateTime("now");
$ystrday = new DateTime("yesterday");
$weekAgo = new DateTime("now")->sub(new DateInterval('P7D'));
$inputDate = new DateTime(whenever);

if($yesterday < $inputDate and $inputDate < $now){
    $outDate = date('g:ia', $inputDate->getTimestamp() );
}else if($weekAgo < $inputDate and $inputDate < $now){
    $outDate = date('l', $inputDate->getTimestamp() );
}else if($inputDate < $weekAgo){
    $outDate = date('d/m/y', $inputDate->getTimestamp() );
}

这还没有经过测试,你需要将你的 mySql 日期放入 php DateTime 对象中,但它应该让你非常接近。

You just have to use a conditional:

$now = new DateTime("now");
$ystrday = new DateTime("yesterday");
$weekAgo = new DateTime("now")->sub(new DateInterval('P7D'));
$inputDate = new DateTime(whenever);

if($yesterday < $inputDate and $inputDate < $now){
    $outDate = date('g:ia', $inputDate->getTimestamp() );
}else if($weekAgo < $inputDate and $inputDate < $now){
    $outDate = date('l', $inputDate->getTimestamp() );
}else if($inputDate < $weekAgo){
    $outDate = date('d/m/y', $inputDate->getTimestamp() );
}

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.

无边思念无边月 2024-11-08 21:57:15

我假设你正在谈论 MySQL TIMESTAMP数据类型,因为我不认为 MySQL 实际上有一个像 Unix 时间戳这样的数据类型(即自纪元以来的秒数),因此您必须首先使用 strtotime 函数

$timestamp = strtotime($dbTimestamp);

这将返回一个 Unix 时间戳 你可以使用。

接下来,我们将定义更多时间戳来与该值进行比较:

首先,我们想知道今天早上午夜的时间戳。为此,您需要将字符串“today”传递给 strtotime

$today = strtotime("today");

接下来,我们需要知道 7 天前的时间戳。您必须在“1 周前”“1 周前午夜” 之间进行选择。两者之间的区别在于,midnight 将返回当天上午 12 点的时间戳,而没有它的版本将返回 7 天前的当前时间(例如今天、区别在于 midnight 将在 4 月 7 日上午 12 点返回,而非午夜版本将在 4 月 7 日下午 3:45 返回):(

$weekAgo = strtotime("1 week ago midnight");

注意,有 许多 strtotime 能够理解的格式,包括许多 相对格式,例如上面使用的“今天”和“1 周前”示例。)

接下来,我们需要定义日期格式 在每种情况下使用:

$timeOnly = "g:i A"; // This gives an "hour:minute AM/PM" format, e.g. "6:42 PM"
$dayOfWeek = "l"     // Gives a full-word day of the week, e.g. "Sunday"
$mdy = "m/d/Y"       // gives two-digit month and day, and 4-digit year,
                     // separated by slashes, e.g. "04/14/2011"

最后,我们只需进行比较,并使用 date 函数

if ($timestamp >= $today) {
  $date = date($timeOnly, $timestamp);
} elseif ($timestamp >= $weekAgo) {
  $date = date($dayOfWeek, $timestamp);
} else {
  $date = date($mdy, $timestamp);
}

这将为您留下一个名为 $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 the strtotime function:

$timestamp = strtotime($dbTimestamp);

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:

$today = strtotime("today");

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 that midnight 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 that midnight will return 12 AM on April 7 and the non-midnight version would, right now, return 3:45PM on April 7):

$weekAgo = strtotime("1 week ago midnight");

(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:

$timeOnly = "g:i A"; // This gives an "hour:minute AM/PM" format, e.g. "6:42 PM"
$dayOfWeek = "l"     // Gives a full-word day of the week, e.g. "Sunday"
$mdy = "m/d/Y"       // gives two-digit month and day, and 4-digit year,
                     // separated by slashes, e.g. "04/14/2011"

Finally, we just do our comparisons, and format our timestamp using the date function:

if ($timestamp >= $today) {
  $date = date($timeOnly, $timestamp);
} elseif ($timestamp >= $weekAgo) {
  $date = date($dayOfWeek, $timestamp);
} else {
  $date = date($mdy, $timestamp);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文