Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/225244-having-a-problem-formatting-a-date/
Share on other sites

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 

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

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! 

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! 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.