How I do convert this date string in PHP?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

So, I am working with an API that outputs dates like this: 2010-07-20T13:57:11+0000 But my application requires that dates be in RFC822 format. Therefore I am trying to convert like so...

PHP Code: [ Select ]
<?php $date = date(DATE_RFC822, $date_string); ?>


However, this outputs the date as: Wed, 31 Dec 69 16:33:30 -0800 Which of course isn't correct at all. I have tried using str_replace() to remove the T, but that doesn't change anything. Any help would be greatly appreciated!

[edited for spelling]
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

PHP Code: [ Select ]
<?php echo date("Y-m-d G:i:s O",strtotime($date_string)); ?>


I can't put the letter "T" in there as that is used for something else (Timezone abbreviation)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I think the problem boils down to the date functions second argument expecting a UNIX timestamp as an integer, and your $date_string variable is a string in the format you're trying to convert from.

Looking at both of your guys posts, I'm thinking continue to use the DATE_RFC822 predefined date constant for the first argument, and go with RTs strtotime solution for getting the second argument converted to a UNIX timestamp for date to work with.

I wonder how hard it would be to patch PHP so that the date function accepts mixed data types for the second argument. (not as a solution to this case, but in general) :scratchhead:
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

PHP Code: [ Select ]
<?php echo date(DATE_RFC822,strtotime($date_string)); ?>


You could prolly get away with RFC2822 though, it technically is supposed to obsolete RFC822...

PHP Code: [ Select ]
<?php echo date("r",strtotime($date_string)); ?>



righteous_trespasser wrote:
I can't put the letter "T" in there as that is used for something else (Timezone abbreviation)


If you want to put a reserved character in there, you can just backslash it like this...

PHP Code: [ Select ]
<?php echo date("Y-m-d\TG:i:s O",strtotime($date_string)); ?>


joebert wrote:
I wonder how hard it would be to patch PHP so that the date function accepts mixed data types for the second argument. (not as a solution to this case, but in general) :scratchhead:


lol why?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

You wouldn't like to be able to pass date() a timestamp, OR a date string and have it work, PH ? How many times have you had to convert a date to a timestamp just to convert it to another formatted string less than a second later ? :D
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

so basically the strtotime() function must be a default to the date() function's second parameter?

So if I have the following:
PHP Code: [ Select ]
echo date("Y-m-d G:i:s","2010-05-06");


PHP must convert that to:
PHP Code: [ Select ]
echo date("Y-m-d G:i:s",strtotime("2010-05-06"));
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I looked at the PHP date functions source. The date() function is itself a wrapper to a php_date function.

It doesn't seem like it should be too much of a hassle to modify that wrapper so it accepts $mixed for the second argument instead of just an integer. Then it can detect whether it's a string or an integer. If it's an integer pass it through normally. If it's a string, retrieve an integer through the behind the scenes strtotime function.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Oh I understand what you mean now.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 82 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.