Erwinator Posted November 17, 2022 Share Posted November 17, 2022 Hi everyone, new here, I need to convert this text to 24 HR format, keeping the format like this "Monday 9:00,12:00,13:00,17:30|Tuesday, 9:00,12:00,13:00,17:30| Wednesday, 9:00,12:00,13:00,17:30|... Closed days remain the same This is the information I scraped already: Saturday, Closed; Sunday, Closed; Monday, 9AM to 12PM, 1 to 5:30PM; Tuesday, 9AM to 12PM, 1 to 5:30PM; Wednesday, 9AM to 12PM, 1 to 5:30PM; Thursday, 9AM to 12PM, 1 to 5:30PM; Friday, 9AM to 12PM, 1 to 5:30PM I have been trying with multiple replaces and date functions but got nowhere since I am a newbie on PHP This is what I am working with so far function weekday( $weekdays) { $weekdays = "Saturday, Closed; Sunday, Closed; Monday, 9AM to 12PM, 1 to 5:30PM; Tuesday, 9AM to 12PM, 1 to 5:30PM; Wednesday, 9AM to 12PM, 1 to 5:30PM; Thursday, 9AM to 12PM, 1 to 5:30PM; Friday, 9AM to 12PM, 1 to 5:30PM"; if (empty( $atts['name'] )) $sunday = explode(';', $weekdays)[0]; $monday = explode(';', $weekdays)[1]; $tuesday = explode(';', trim(explode(';', $weekdays)[2]))[0]; $wednesday = explode(';', trim(explode(';', $weekdays)[3]))[0]; $thursday = explode(';', trim(explode(';', $weekdays)[4]))[0]; $friday = explode(';', trim(explode(';', $weekdays)[5]))[0]; $saturday = explode(';', trim(explode(';', $weekdays)[6]))[0]; echo date('l, G:i", $sunday.)'<br>'; echo date('l, G:i", $monday).'<br>'; echo date('l, G:i", $tuesday.)'<br>'; echo date('l, G:i", $wednesday.)'<br>'; echo date('l, G:i", $thursday.)'<br>'; echo date('l, G:i", $friday.)'<br>'; echo date('l, G:i", $saturday.)'<br>'; } Thank you for the help Quote Link to comment https://forums.phpfreaks.com/topic/315548-need-help-formatting-week-day-and-time/ Share on other sites More sharing options...
ginerjm Posted November 18, 2022 Share Posted November 18, 2022 I"m not seeing the original source text that you want to work on..... Quote Link to comment https://forums.phpfreaks.com/topic/315548-need-help-formatting-week-day-and-time/#findComment-1602705 Share on other sites More sharing options...
ginerjm Posted November 18, 2022 Share Posted November 18, 2022 (edited) My critique of your code: - you have a function with an incoming argument but you don't even look at what it may be. You simply load it up with a string that follows no pattern. - then you work on a var named $atts which is supposed to be an array. But where is this defined? It's not available in your function - you have a series of similar looking lines of code following the bad reference to $atts but only one of them will be executed even if you define that $atts var somehow. Read up on how the if statement is used. - You are then trying to reference $weekdays as if it were an array. It's not. It is a string. The rest of the code is useless until you fix what I have gone over. And just why is this input string/data arranged like that? Are you producing this or are you getting it from somewhere that you have no control over? Either way your work would be much easier if you rearranged the input data. Edited November 18, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/315548-need-help-formatting-week-day-and-time/#findComment-1602708 Share on other sites More sharing options...
ginerjm Posted November 18, 2022 Share Posted November 18, 2022 This took me some time but here is what I wrote: $weekdays = "Saturday, Closed;Sunday, Closed;Monday, 9AM to 12PM, 1 to5:30PM;Tuesday,9AM to 12PM, 1 to 5:30PM;Wednesday, 9AM to 12PM, 1 to 5:30PM;Thursday, 9AM to 12PM, 1 to 5:30PM;Friday, 9AM to 12PM, 1 to 5:30PM"; $days = explode(";", $weekdays); echo "Days are<pre>",print_r($days,true),"</pre>"; foreach ($days as $day) $parts[] = explode(",", $day); // now output as Monday 9:00,12:00,13:00,17:30|T $time_str = ''; foreach ($parts as $day) { $str = trim($day[0]) . " "; for( $i=1; $i< count($day); $i++) { $d = trim($day[$i]); if ($d == 'Closed') $str .= " Closed"; else { $times = explode('to',$day[$i]); foreach($times as $t) { $t = trim($t); If(!$time_val = date('G:i',strtotime($t))) { echo __LINE__. "Could not convert $t to date<br>"; exit(); } $str .= $time_val . ","; } } } $time_str .= rtrim($str," ,\t") . '|'; } echo "Result is <br>$time_str<br>"; And here is what it produces: Days are Array ( [0] => Saturday, Closed [1] => Sunday, Closed [2] => Monday, 9AM to 12PM, 1 to5:30PM [3] => Tuesday,9AM to 12PM, 1 to 5:30PM [4] => Wednesday, 9AM to 12PM, 1 to 5:30PM [5] => Thursday, 9AM to 12PM, 1 to 5:30PM [6] => Friday, 9AM to 12PM, 1 to 5:30PM ) Result is Saturday Closed|Sunday Closed|Monday 9:00,12:00,19:00,17:30|Tuesday 9:00,12:00,19:00,17:30|Wednesday 9:00,12:00,19:00,17:30|Thursday 9:00,12:00,19:00,17:30|Friday 9:00,12:00,19:00,17:30| You have a data issue where you don't specify a recognizable time value for the 1 o'clock start times. Quote Link to comment https://forums.phpfreaks.com/topic/315548-need-help-formatting-week-day-and-time/#findComment-1602711 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.