jeffkas Posted January 21, 2011 Share Posted January 21, 2011 Hi all.. I'm grabbing the following date format from an rss feed: Sun, 16 Jan 2011 00:00:00 -0800 What's the best way to strip away the first 5 characters (Sun, ) and the last 15 characters ( 00:00:00 -080)... but still keeping this as a date (not a string) to store in my database? Thanks in advance for any ideas! Quote Link to comment https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/ Share on other sites More sharing options...
taquitosensei Posted January 21, 2011 Share Posted January 21, 2011 probably substr the last 15 $date="Sun, 16 Jan 2011 00:00:00 -0800"; $date=substr($date,-15); /// this would give you "Sun, 16 Jan 2011" $date=substr($date,0,5); // "16 Jan 2011" $timestamp=strtotime($date); then you can format it and do whatever you want from there $formatted_date=date("Y-m-d", $timestamp); // 2011-01-16 Quote Link to comment https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/#findComment-1163266 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 strtotime will take that whole mess and parse it without issue . . . $date="Sun, 16 Jan 2011 00:00:00 -0800"; $formatted_date=date("Y-m-d", strtotime($date)); echo $formatted_date; // 2011-01-16 Quote Link to comment https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/#findComment-1163273 Share on other sites More sharing options...
taquitosensei Posted January 21, 2011 Share Posted January 21, 2011 I guess you learn something new everyday. Quote Link to comment https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/#findComment-1163279 Share on other sites More sharing options...
jeffkas Posted January 21, 2011 Author Share Posted January 21, 2011 Well my code was much like the first reply. I thought I was doing something wrong but apparently that's not the problem. I have a very strange oddity. My code is this: $date = substr($date, 6, 18); $date = strtotime($date); $date = date( 'y-m-d', $date ); But... I can't for the life of me figure out why I can't capture any double-digit day for 2011! I'm grabbing contents for the last 2 months.. the rss feed has plenty of dates from 10 - 31, and they're saved for December in mysql, but mysql replaces any January day of 10 till now with a zero... so 11 becomes 01, 12 becomes 02, etc... Anyway.. thanks for the help. Not much you can do for my other issue without seeing code. I'm just venting. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/#findComment-1163289 Share on other sites More sharing options...
jeffkas Posted January 21, 2011 Author Share Posted January 21, 2011 strtotime will take that whole mess and parse it without issue . . . $date="Sun, 16 Jan 2011 00:00:00 -0800"; $formatted_date=date("Y-m-d", strtotime($date)); echo $formatted_date; // 2011-01-16 IT WORKED!! Thank You!! I cannot figure out why the other code wouldn't.. sure looks like it should, but I tried this before hanging it up for the day and voila! $date=date("Y-m-d", strtotime($date)); I can't believe that's smart enough to strip the string like that. Love it! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/#findComment-1163302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.