1

I am trying to get the date from a <time> tag

$explode = explode( '<time class="abs">', file_get_contents($url))[1];
$get_date = explode("</time>" , $explode);
echo date_format(date_create($get_date[0]),"Y/m/d");

The echo shows the date but there is also an error

Notice: Undefined offset: 1

When I do a var_dump, it shows that there is nothing in the array but then why does it echo the correct date..?

BragDeal
  • 748
  • 2
  • 10
  • 25
  • bad approach to scrapeing HTML :http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php –  Jul 09 '15 at 20:45
  • can't see how this'd issue an `offset: 1`, since you're only accessing offset `0`. Have you tried `var_dump($explode, $get_date)` to see if the explode is working properly? and technically speaking, you should be using a DOM parser for this. – Marc B Jul 09 '15 at 20:45
  • of course, you're using `date_create()` wrong. it's `date_create($time_value, $time_zone)`. `Y/m/d` is **NOT** a timezone. – Marc B Jul 09 '15 at 20:46
  • @MarcB Look the the first code line to the right at the end :) -> `file_get_contents($url))` **[1]** – Rizier123 Jul 09 '15 at 20:47
  • @Rizier123: ah yeah. my bad. – Marc B Jul 09 '15 at 20:47
  • @Dagon i just dont want to use any classes that are not my own so this is the only solution i came up with – BragDeal Jul 09 '15 at 20:50
  • is there a way to fix the error? i mean it echo's what i need, just the error message is irritating – BragDeal Jul 09 '15 at 20:51
  • did you write explode? file_get_contents? no? so you already are. DOM is as inbuilt as explode et al –  Jul 09 '15 at 20:51
  • Depending on your version of PHP using the array accesser directly on the function may be the issue. – Christian Jul 09 '15 at 20:52
  • @dagon yeah but I don't have to license anything. I am using the latest PHP version – BragDeal Jul 09 '15 at 20:53
  • first 4 items on the linked post are native php. all 100% better than scrapeing your way –  Jul 09 '15 at 20:54
  • @Dagon so what should i do? I am not too sure how to use DOMs – BragDeal Jul 09 '15 at 21:04

0 Answers0