0

I'm getting an external RSS Feed Post in different website, what I noticed is that they all have a different format of Publish Date. Some uses a formatting like this:

2012-04-09T08:23:07Z

Fri, 06 Apr 2012 01:25:43  0000

There are still lots of format but I was just showing you two examples.

Now, I want everything to have a uniform format, so i'm using strtotime(). Here's my code:

$date = "2012-04-09T08:23:07Z";
date( "F d, Y", strtotime( $date ) );
/* Result: April 09, 2012 */

My problem here is the second format Fri, 06 Apr 2012 01:25:43 0000

There is a 0000 at the last which makes the strtotime() act weird.

$date = "Fri, 06 Apr 2012 01:25:43  0000";
 date( "F d, Y", strtotime( $date ) );
 /* Result: April 06, 0000 */

Obviously the result should be April 06,2012.

My question here is that how can I not let strtotime() act weird when it detects a format that has a 0000 at the last. Any idea how to do this? Your help would be greatly appreciated! Thanks! :)

Thanks!

1
  • 1
    Did you find any other datetime formats that cause a problem? If this is the only one, you may just have to special case strings that end in "0000". Commented Apr 23, 2012 at 4:00

3 Answers 3

1

I'm pretty sure you have a formatting issue:

Fri, 06 Apr 2012 01:25:43  0000

Notice the two spaces before 0000? That's not a coincidence. The original is probably this:

Fri, 06 Apr 2012 01:25:43 +0000

Which is generated with gmdate(DATE_RFC822). In URI's, the + symbol often gets translated to a space. I think that's your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I noticed that one too, there's a + symbl before the 0000, but when I stored it in the database the + symbol is gone, is it because I tried to mysql_real_escape_string() it?
Nope, mysql_real_escape_string doesn't escape + characters, so perhaps this happens when you extract it from the XML?
1

How about just stripping the "0000"? Something like:

$date=str_replace("0000", "", $date);

1 Comment

RFC822 doesn't indicate GMT as a prerequisite, so it could also end with 0800.
1

try this:

<?php 
$date = "Fri, 06 Apr 2012 01:25:43 0000";

 if (strtotime($date) !== false) 
 {
 $timestamp = strtotime($date);
     echo date( "F d, Y", $timestamp );
 } 
 else 
 {
     echo "error";
 }

 ?>

for $date = "Fri, 06 Apr 2012 01:25:43 0000"; outputs error.

$date = "Fri, 06 Apr 2012 01:25:43"; gives output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.