bschultz Posted March 13, 2020 Share Posted March 13, 2020 I have a given date on a webpage, that I'm scraping to insert into a DB. The date is in this format: Sun, Feb 9<br />3:00 PM ET I need to insert this into the DB in this format: 2020-02-09 15:00:00 It will always be this year...How can I change this data to be inserted correctly? I'm trying this...and it's inserting as 1969 $date = 'Sun, Feb 9<br />3:00 PM ET': $healthy = array("<br />", " ET", "Sun.", "Mon.", "Tue.", "Wed.", "Thu", "Fri.", "Sat.", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); $yummy = array(" ", "2020", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $date = str_replace($healthy, $yummy, $date1); $start = date('Y-m-d H:i:s', strtotime("$date -120 minutes")); Thanks! Quote Link to comment Share on other sites More sharing options...
chhorn Posted March 13, 2020 Share Posted March 13, 2020 (edited) Looks like you just have to skip some parts var_dump(new DateTime('Sun, Feb 9 3:00 PM')); /** object(DateTime)#1 (3) { ["date"]=> string(26) "2020-02-09 15:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } then modify() and format(). Edited March 13, 2020 by chhorn Quote Link to comment Share on other sites More sharing options...
Barand Posted March 13, 2020 Share Posted March 13, 2020 $date = 'Sun, Feb 9<br />3:00 PM ET'; $date = str_replace('<br />', ' ', $date); $dtobj = DateTime::createFromFormat('D, M j g:i A *', $date); echo $dtobj->format('Y-m-d H:i:s'); // 2020-02-09 15:00:00 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.