-1

The RSS feed I read from was recently updated and messed up quite a few things, I fixed most of the issues but this I can't figure.

How the date was formatted previously:

<div>
    <div>
        <div>
            13 </div>
    </div>
</div>
<div>
    <div>
        <div>
            July </div>
    </div>
</div>
<div>
    <div>
        <div>
            2012 </div>
    </div>
</div>

How it is right now:

<div>
    <div>
        <div>13th July, 2012</div>
    </div>
</div>

So:

  1. Day Month and Year has to be separated in their own DIVs (most difficult)
  2. "th" and "," needs to be removed (this is easy with str_replace)

Now the problem is I could do this with jQuery in minutes but it has to be done with PHP before it gets saved, and since the divs have no class/id its even more difficult for me.

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • What do you have so far? – jeroen Nov 19 '12 at 13:53
  • For this problem? Nothing cause I just can't figure how to separate these. Other issues I've fixed and str_replaces are no problem either. – eozzy Nov 19 '12 at 13:54
  • 1
    Couldn't you just use `strip_tags` in PHP to get the date and then use `strtotime` to normalize the date? Then you can generate whatever HTML you want. – Darrrrrren Nov 19 '12 at 13:55
  • This should get you somewhat in the right direction: `explode(',', date('d,F,Y', strtotime('13th July, 2012')));` – Ja͢ck Nov 19 '12 at 13:56

2 Answers2

4

You can use strip_tags in PHP to get the date and then use strtotime to normalize the date.

Then you can generate whatever HTML you want.

EDIT: here is your solution:

<?php
//at this point, $string = the input you wish to modify
$string = "
<div>
    <div>
        <div>13th July, 2012</div>
    </div>
</div>
";

//get normalised time
$time = strtotime(trim(strip_tags($string)));

//with normalised time, generate your new output
echo "
<div>
    <div>
        <div>
            ".date("d",$time)." </div>
    </div>
</div>
<div>
    <div>
        <div>
            ".date("F",$time)." </div>
    </div>
</div>
<div>
    <div>
        <div>
            ".date("Y", $time)." </div>
    </div>
</div>
";
?>
Darrrrrren
  • 5,968
  • 5
  • 34
  • 51
0

Without writing the actual code for you, a solution would be:

  • Use a DOM parser to get the information you need (the date div contents);
  • explode on the space character;
  • (int) the first number and rtrim the month to get rid of the comma.

Or use @Darrrrrren's solution which is way better...

jeroen
  • 91,079
  • 21
  • 114
  • 132