[英]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 . 我倾向于在SQL中进行日期格式化,就像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 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:
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.