php time converter

  • panzhuli
  • Graduate
  • Graduate
  • No Avatar
  • Joined: Oct 14, 2004
  • Posts: 135
  • Loc: Cleveland
  • Status: Offline

Post August 27th, 2010, 6:06 pm

Hi there -

I have to convert a date from an xml feed. The feed comes in like this:

08/23/2010, or DD/MM/YYYY

and they want me to turn it around to MM/DD/YYYY. why they don't just spit it out that way is beyond me... anyways...

Is there a php widget to do that? I'd hate to sniff for the "/" characters and do it that way... I couldn't find anything on php[dot]net that sounded right to me...

thanks!
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post August 27th, 2010, 6:06 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 27th, 2010, 6:35 pm

There's strptime, but there's a bunch of nasty notes on the manual page. If you're on Windows the function isn't even an option.

strtotime seems to be out of the question, I believe it interprets dates in the format you're aiming for, rather than the one you're converting from. I'm not sure whether this is affected by locale settings or not.

You could preg_replace it.
PHP Code: [ Select ]
$date = preg_replace('#(\d{2})/(\d{2})/(\d{2}|\d{4})#', '$2/$1/$3', $str);
Strong with this one, the sudo is.
  • panzhuli
  • Graduate
  • Graduate
  • No Avatar
  • Joined: Oct 14, 2004
  • Posts: 135
  • Loc: Cleveland
  • Status: Offline

Post August 30th, 2010, 10:50 am

this works well! Now they want me to trim the YYYY to just YY. Is there a way to do that in that pattern?
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 30th, 2010, 12:20 pm

PHP Code: [ Select ]
$date = preg_replace('#(\d{2})/(\d{2})/(?:\d{2})?(\d{2})#', '$2/$1/$3', $str);
Strong with this one, the sudo is.
  • panzhuli
  • Graduate
  • Graduate
  • No Avatar
  • Joined: Oct 14, 2004
  • Posts: 135
  • Loc: Cleveland
  • Status: Offline

Post August 30th, 2010, 12:25 pm

impressive. thanks! someday i'll understand what that pattern says lol
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post August 30th, 2010, 1:24 pm

It says "find two digits and save them as $1, then find a forward slash, then find two digits and save them as $2, then find a forward slash, look for two digits but don't fail if you don't find them and don't remember them if they're there, then find two digits and remember them as $3".

In the second argument of preg_replace it says "replace the entire string that was matched by the pattern with the value of $2, followed by a forward slash, followed by the value of $1, followed by a forward slash, and finally the value of $3".
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: ScottG and 239 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.