简体   繁体   English

在PHP中从SQL格式化时间戳的最简单方法是什么?

[英]What is the simplest way to format a timestamp from SQL in PHP?

What is the simplest, fastest way to complete the PHP code below such that the output is in a user-friendly format (for example, "October 27, 2006")? 什么是最简单,最快速的方法来完成下面的PHP代码,使输出采用用户友好的格式(例如,“2006年10月27日”)?

$result = mysql_query("SELECT my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = ???($row['my_timestamp']);
echo $formatted_date;

You could use MySQL to do this for you, 您可以使用MySQL为您执行此操作,

$result = mysql_query("SELECT DATE_FORMAT(my_timestamp, '%M %d, %Y) AS my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = $row['my_timestamp'];
echo $formatted_date;

Or use PHP, 或者使用PHP,

$result = mysql_query("SELECT my_timestamp FROM some_table WHERE id=42", $DB_CONN);
$row = mysql_fetch_array($result);
$formatted_date = strftime('%B %d, %y', $row['my_timestamp']);
echo $formatted_date;

I tend to do the date formatting in SQL, like Aron 's answer . 我倾向于在S​​QL中进行日期格式化,就像Aron回答一样 Although for PHP dates, I prefer using the DateTime object (PHP5+) over date : 虽然PHP日期,我更喜欢使用的DateTime对象(PHP5 +)在日期

$timestamp = new DateTime($row['my_timestamp']);
echo $timestamp->format('F j, Y') . '<br />';
echo $timestamp->format('F j, Y g:ia');

You want the date() function. 你想要date()函数。

If you've got a DATE or DATETIME column, you can use SELECT UNIX_TIMESTAMP(mycolumn) AS mycolumn to convert it to a unix timestamp for the date function to use. 如果您有DATE或DATETIME列,则可以使用SELECT UNIX_TIMESTAMP(mycolumn)AS mycolumn将其转换为要使用的日期函数的unix时间戳。

You should definitely use the DateTime class (or any home grew equivalent class), over de Date function, and not use timestamps: 你应该使用DateTime类(或任何home增长的等效类),over de Date函数,而不是使用timestamps:

  • Timestamp don't work well in all environments for dates before 1970 - if you deal with birthdays, you're relying on code that may break on some servers 对于1970年以前的日期,时间戳在所有环境中都不能正常工作 - 如果您处理生日,那么您依赖的代码可能会在某些服务器上中断
  • Be also very carefull of the use of strftime, it looks like a nice function, but it is very unrelyable, as it depends on setlocale, which is process-wide. 也非常小心strftime的使用,它看起来像一个很好的功能,但它是非常不可靠的,因为它取决于setlocale,这是在整个流程。 What this means is that if your server is a windows box, you have one process per processor, and then the rest is multi-threaded - in other words, one setlocale in one script will affect the other scripts on the same processor - very nasty ! 这意味着如果你的服务器是一个Windows框,你每个处理器有一个进程,然后其余部分是多线程的 - 换句话说,一个脚本中的一个setlocale将影响同一处理器上的其他脚本 - 非常讨厌!

At the end of the day, don't rely on timestamps, unless you are in an english only environment, and deal with dates between 1970 and 2032 only...! 在一天结束时,不要依赖时间戳,除非你是在英语环境中,并且只处理1970年到2032年之间的日期......!

If you have your tables indexed on that date field and you use a mysql data format function in your call.. (ie .. SELECT DATE_FORMAT(my_timestamp, '%M %d, %Y) AS my_time ) it will destroy your indexing. 如果您的表在该日期字段上编入索引,并且您在调用中使用了mysql数据格式函数..(即.. SELECT DATE_FORMAT(my_timestamp,'%M%d,%Y)AS my_time)它将破坏您的索引。 Something to keep in mind. 要记住的事情。 We have seen dramatic increases in speed in removing all functioning from our sql statements and letting php handle it all. 我们已经看到从sql语句中删除所有功能并让php处理所有功能的速度显着提高。 Functioning such as formatting dates and simple math 功能如格式化日期和简单的数学

I use: 我用:

date("F j, Y", strtotime($row['my_timestamp'])) 日期(“F j,Y”,strtotime($ row ['my_timestamp']))

or you can change the SELECT to: DATE_FORMAT(field,'%d %M, %Y') as datetime 或者您可以将SELECT更改为:DATE_FORMAT(字段,'%d%M,%Y')作为日期时间

You have a choice. 你有一个选择。 You can use the date() function in PHP and process the output from MySQL, or you can use the date_format() function in MySQL and have it return a formatted string to begin with. 您可以在PHP中使用date()函数并处理MySQL的输出,或者您可以在MySQL中使用date_format()函数并让它返回一个格式化的字符串。

In my experience is that it doesn't really matter which you use, but BE CONSISTENT. 根据我的经验,你使用哪个并不重要,但要保持一致。 They have different formatting parameters, so if you use both in a given application you'll spend a lot of time trying to remember if 'W' gives you the name of the day of the week or the week of the year. 它们具有不同的格式参数,因此如果您在给定的应用程序中同时使用它们,您将花费大量时间来记住“W”是否为您提供了星期几或一年中的星期的名称。

Have the database do the formatting for you. 让数据库为您执行格式化。 It is far less susceptible to error because you are not reading a string and converting it. 它不容易出错,因为你没有读取字符串并进行转换。 Instead, the database is going from its native format to a string. 相反,数据库将从其本机格式转换为字符串。

暂无
暂无

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

相关问题 有没有一种简单的方法可以从php中的SQL时间戳获取Unix时间戳? - Is there an easy way to get a Unix timestamp from an SQL timestamp in php? 在PHP中实现Parallel cURL的最简单方法是什么? - What is the simplest way to implement Parallel cURL in PHP? 在PHP中引用多字节字符串的最简单方法是什么? - What is the simplest way to reference a Multibyte string in PHP? 将php代码定义为变量-最简单的方法是什么? - Defining php code as a variable - what is the simplest way? 从Timestamp php中减去6个月的最简单方法是什么 - What is the easiest way to substract 6 months from Timestamp php 使用PHP从SQL数据库中的jQuery通过jQuery使用AJAX调用更新输入字段的最简单方法 - Simplest way to update an input field using an AJAX call through jQuery from an SQL database using PHP 从PHP Google API获取电子邮件地址的最简单方法是什么 - What is the simplest possible way to grab an email address from the PHP Google API 在来自 PHP 对象的 GET 请求中防止错误返回的最简单方法是什么 - What is the simplest way to prevent Error return In a GET request from an object in PHP 将计划的XML解析为SQL数据库的最简单方法是什么? - What is the simplest way to make scheduled XML parsing into an SQL database? 在自定义的magento 2模块中使用外部php库的最简单方法是什么? - What is the simplest way to use an external php library in a custom magento 2 module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM