0

I need some help with my code, I have a trouble with checking on two different strings as I want to check if the string in the variable $program_times are equal to PM and $next_program_times are equal to AM. When I try it, it will show nothing and it won't do anything at all.

When I tried this:

if(strpos($program_times, 'PM') !== false && strpos($next_program_times, 'AM') !== false) 
{
  echo 'hello 4';
}

And I have also tried this:

if (strpos($next_program_times, 'AM') !== false) 
{
  echo 'hello 3';
}

Here is the full code:

for ($jj = 1; $jj <= 113; $jj++)
{
  $program_title = $html->find("li[id=row$ii-$jj]", 0)->plaintext; // with this
  $program_title = preg_split("/ {6,}/",$program_title);
  $program_times = $program_title[1];

  if (!empty($program_titles)) 
  {
    $program_times = (new Datetime($program_times))->add(new DateInterval('PT5H'))->format('g:i A');
    echo '<span id="time', $show_id, '">', $program_times, '</span> ', '<span id="title', $show_id, '">', $program_titles, $program_bbf, $program_cat, '</span> <br></br>';

    $next_program_times = $program_times + 1;

    if (strpos($next_program_times, 'AM') !== false) 
    {
      echo 'hello 3';
    }


    if(strpos($program_times, 'PM') !== false && strpos($next_program_times, 'AM') !== false) 
    {
      echo 'hello 4';
    }

Here is the output:

10:00 PM Reba - To Tell the Truth 

10:30 PM Reba - Brock's Mulligan 

11:00 PM Job or No Job - Chicago Restaurants 

12:00 AM Bruce Almighty 

2:00 AM 17 Again 

4:00 AM The 700 Club 

5:00 AM Beetlejuice 

7:00 AM Sexy in 3 Weeks! 

7:30 AM Paid Programming 

8:00 AM The 700 Club 

Can you please show me an example of how I can check the strings between PM and AM before I could do something?

Script47
  • 14,230
  • 4
  • 45
  • 66
Rob
  • 189
  • 1
  • 3
  • 14
  • Is error reporting enabled? – Script47 Aug 06 '15 at 21:10
  • @Script47 no i haven't try it yet. how i can use to check the error? – Rob Aug 06 '15 at 21:13
  • if you haven't tried it yet, how do you know it isn't working? – Script47 Aug 06 '15 at 21:14
  • because i cant have the `echo hello 3` and `echo hello 4` as the output. i need to get them to show on the output so i would know it is works. – Rob Aug 06 '15 at 21:15
  • Have you looked at this http://stackoverflow.com/questions/4366730/check-if-string-contains-specific-words? – Script47 Aug 06 '15 at 21:22
  • I would check using != and not !== because you don't need to check if they are identical but equal. – Lelio Faieta Aug 06 '15 at 21:22
  • Editing my own comments: you need to use !== as mentioned in the So question quoted by @Script47 if the string begins with AM or PM to avoid 0 as position to be misread as 0 as false – Lelio Faieta Aug 06 '15 at 21:26
  • @LelioFaieta can you please show me an example of how i can check on two different strings between `PM` and `AM` to see if it returns to true? – Rob Aug 06 '15 at 21:44
  • @Rob I've posted an answer to this comment. Please have a look. Sorry if I answer only now but I think we are in two different time zones :) – Lelio Faieta Aug 07 '15 at 08:20

2 Answers2

1

Your issue is in the main logic of your code. You are extracting from this:

12:00 AM Bruce Almighty

the first part of the string and you try to check if it contains 'AM' or 'PM'. Since you are extracting a time you can convert it into a date php object instead of handling it as a string. This way you will also get if the $next_program_times is in AM or PM period of time. When you try to add +1 to '12:00 AM' you don't get '1:00 PM'! So I'd go converting the strings to date object like this:

$program_times = strtotime($program_times);
$refer_time = strtotime('01:00 PM');
if($program_times<$refer_time){
    //we are in the AM range
}else{
    //we are in the PM range
}

this assuming you are considering only a 24 hours range so that all the times you evaluat refers to the same day. For the $next_program_times I'd also go like this with a different approach:

$next_program_times = strtotime($program_times)+3600;

and then you can go again with the same if:

if($next_program_times<$refer_time){
    //we are in the AM range
}else{
    //we are in the PM range
}

Note that I'm not very familiar with the meridian representation of time (AM/PM) so maybe I'm not using the very first value of PM in the $refer_time value.

You can find more about php strtotime function here.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
-1

To compare, you can write:

if ($program_times == "PM")

Also strpos returns position of first occurrence of string and not the Boolean value

Bulat
  • 6,869
  • 1
  • 29
  • 52
Pravin
  • 32
  • 6