解析日期字符串 [英] parse a date string
问题描述
我有一个帖子变量中的这个字符串
I have this string in a post variable
'03/21/2011'
我需要通过php解析,并将其转换成这种格式
I need to parse it via php and turn it into this format
'2011-03-21'
我正在使用php和i需要这种格式,所以我可以运行这个查询
I am using php and i need this format so i can run this query
SELECT prospect as 'Prospect', company as 'Company', industry as 'Industry', created_at as 'Original Date Submitted', software as 'Software', warm_transfer as 'Warm Transfer', UserName as 'Sales Rep' FROM mercury_leads join user on UserID=user_id WHERE created_at BETWEEN '2011-01-01' AND '2011-03-22'
推荐答案
如果你想处理它在PHP中,最好的办法是使用 strtotime()
函数,它将一个字符串转换为一个时间,然后可以使用< a href =http://us2.php.net/manual/en/function.date.php =nofollow> date()
功能。
If you want to handle it in PHP, your best bet is to use the strtotime()
function which converts a string into a time that can then be manipulated with the date()
function.
所以你可以这样做:
$dateStr = date('Y-m-d', strtotime('03/21/2011'));
使用 strtotime()
你不必担心你传递的字符串的确切格式。只要它是半合理的,它将会转换它。
The nice thing about using strtotime()
is that you don't have to worry about the exact format of the string you pass in. As long as it's something semi-reasonable, it'll will convert it.
所以它会处理 03/21/2011 , 3-21-2011 , 03- 21-11 ,无需任何修改或特殊情况。
So it would handle 03/21/2011, 3-21-2011, 03-21-11 without any modifications or special cases.
这篇关于解析日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!