1

Ive been stuck on this, relatively simple problem for too long now.

  1. I scrape a site and get date in this format Fri, Dec 1, 2017
  2. I then use substr($date, 5); to remove the day name. which returns the following: Dec 1, 2017
  3. I then use str_replace($new_date, ',',' '); where I want to remove the comma , and replace it with white space. So that I have a date format of Dec 1 2017

Ultimately what Im trying to do is convert the date Dec 1 2017 to the format date('Y-m-d) but before I can do that I first need to figure out what exactly is wrong with code below.

Code

 $date = $cells2->item(0)->textContent;
        $new_date = substr($date, 5);
        $new_date = str_replace($new_date, ',',' ');
        echo $new_date;

Problem

My error is in step 3 str_replace() the problem is it is real hard to figure out what exactly is wrong since when I echo $new_date nothing gets displayed on the screen.

Would greatly appreciate it if someone more experienced can give my code a scan, and provide some advice.

Many Thanks.

Marilee
  • 1,598
  • 4
  • 22
  • 52
  • 1
    Why are you using str_replace() when PHP has built in functions just for this purpose? – John Conde Dec 18 '17 at 02:44
  • `$new_date = str_replace(',', ' ', $new_date);` is the correct syntax. `str_replace(search, replace, subject)` – Goma Dec 18 '17 at 02:45
  • @JohnConde do you perhaps have the name of the function? – Marilee Dec 18 '17 at 02:47
  • @Erwin wrong syntax. Ouch Thanks. If you add it as answer I'll accept. – Marilee Dec 18 '17 at 02:48
  • Doesn't matter. Just clearing up. But i also recommend doing what @JohnConde suggests. Or check the duplicate link :) – Goma Dec 18 '17 at 02:50
  • @Erwin just to clarify what @JohnCode is suggesting is that I can directly convert to `Fri, Dec 1, 2017` to the format `2017-12-01` using a build in function...? – Marilee Dec 18 '17 at 02:55
  • @Erwin im seeing the duplicate link. Many thanks guys – Marilee Dec 18 '17 at 02:56
  • echo date('M j Y', strtotime($date)); should be the correct way to do this. The date() function reference has it all : http://php.net/manual/en/function.date.php – Amila K. Dec 18 '17 at 02:57

0 Answers0