Parse Date-time From String With PHP & MySQL
2011-10-24 00:17
549 查看
STR_TO_DATE:
You need to tell MySQL how to parse the string, and you do that byfiguring out the pattern and pass it to STR_TO_DATE() as the second parameter.
DATE_FORMAT:
This function is used to print the 'DATE' object in the way you want, you still need to tell the pattern to DATA_FORMAT(). The only one point is that you should pass a 'DATE' type variable to
it, instead of a string, so you might need to convert the type before that, however, there are exceptions around.
The query I design:
This is the normal way of thinking, but actually, MySQL use '0000-00-00' as format of date, so the string follows this format can be used as DATE type implicitly. That means, the STR_TO_DATE can be eliminated from our above query
statement, and become:
Here is the performance comparison of the two methods, may be due to the data volume is too small, the time consumed is the same:
REFS:
http://stackoverflow.com/questions/3296725/parse-date-in-mysql
http://www.roseindia.net/sql/sqldate/mysql-date-parse.shtml
http://stackoverflow.com/questions/1279061/php-parse-date-string
http://php.net/manual/en/book.datetime.php
You need to tell MySQL how to parse the string, and you do that byfiguring out the pattern and pass it to STR_TO_DATE() as the second parameter.
DATE_FORMAT:
This function is used to print the 'DATE' object in the way you want, you still need to tell the pattern to DATA_FORMAT(). The only one point is that you should pass a 'DATE' type variable to
it, instead of a string, so you might need to convert the type before that, however, there are exceptions around.
MySQL Official Online Doc: 11.7. Date and Time Functions
The above doc describes all the functions that work on DATE & TIME.The query I design:
SELECT MONTH( STR_TO_DATE( m_date, '%Y-%m-%d' ) ) AS mon, DATE_FORMAT( STR_TO_DATE( m_date, '%Y-%m-%d' ) , '%d/%m/%Y' ) AS DATE, DAYOFWEEK( STR_TO_DATE( m_date, '%Y-%m-%d' ) ) AS dayweek, team2_id, team1_id, score1, score2, m_time, m_location, m_remark FROM jos_bl_match WHERE MONTH( STR_TO_DATE( m_date, '%Y-%m-%d' ) ) =9 ORDER BY m_date
This is the normal way of thinking, but actually, MySQL use '0000-00-00' as format of date, so the string follows this format can be used as DATE type implicitly. That means, the STR_TO_DATE can be eliminated from our above query
statement, and become:
SELECT MONTH( m_date ) AS mon, DATE_FORMAT( m_date , '%d/%m/%Y') AS date, DAYOFWEEK( m_date) AS dayweek, team2_id, team1_id, score1, score2, m_time, m_location, m_remark FROM jos_bl_match WHERE MONTH( m_date ) = 9 ORDER BY m_date
Here is the performance comparison of the two methods, may be due to the data volume is too small, the time consumed is the same:
REFS:
http://stackoverflow.com/questions/3296725/parse-date-in-mysql
http://www.roseindia.net/sql/sqldate/mysql-date-parse.shtml
http://stackoverflow.com/questions/1279061/php-parse-date-string
http://php.net/manual/en/book.datetime.php
相关文章推荐
- (pbc lua 加入)c++_lua_Python with/without extension性能测试 (10万次SerializeToString & ParseFromString)
- Mysql unix_timestamp() FROM_UNIXTIME和DATE_FORMAT(date,format)
- PHP Tutorials : Add, View, Edit & Delete from to MySQL database, Live Example (News System)
- mysql时间格式函数:from_unixtime()、unix_timestamp()、date_format()
- 跟燕十八学习PHP-第二十三天-mysql日期时间列详解date-time-year-timesta
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- 【mysql】Date和String的互相转换(DATE_FORMAT & STR_TO_DATE)
- php 解决MySQL插入数据出现 Incorrect string value: '\xF0\x9F\x92\x8BTi...'错误
- error: Error: String types not allowed (at 'fromAlpha' with value '0.0f').
- mysql 格式化日期 DATE_FORMAT,FROM_UNIXTIME,UNIX_TIME等
- 【mysql】Date和String的互相转换(DATE_FORMAT & STR_TO_DATE)
- 解决 "unable to convert MySQL date/time value to System.DateTime"
- 'Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
- MYSQL DATE_FORMAT以及UNIX_TIMESTAMP 和FROM_UNIXTIME 函数整理
- org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.util.Date from String value '2012-12-12 12:01:01': not a valid representation (error: Can not parse date "2012-12-
- Mysql日期时间类型(DATE,DATETIME,TIMESTAMP)和函数及与linux系统时间之间的转换(UNIX_TIMESTAMP、FROM_UNIXTIME)
- XML DOMParser parseFromString特殊字符"&" "<"处理
- 关于DataTime的问题 对比ToShortDateString()和ToString("yyyy-MM-dd")
- time value from 1970 to date string.
- unable to convert MySQL date/time value to System.DateTime