0

Hey guys,
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?

2
Contributors
3
Replies
25
Views
2 Years
Discussion Span
Last Post by cereal
0

Hi, the issue is at line 12:

$next_program_times = $program_times + 1;

If $program_times has value of 3:00 PM which is a string and you add an integer, you get only 4, so PM does not exists anymore, nor the formatting. To keep the current format, instead, you have to use the DateInterval class, as done on line 9:

$next_program_times = (new Datetime($program_times))->add(new DateInterval('PT1H'))->format('g:i A');

Which will output 4:00 PM.

Edited by cereal

0

@cereal: thank you very much for this, i can see it is almost working but it won't work when i have time more than 2 hours, so i think it would be a good idea to get the tag id.

when i try this:

$next_program_times = '<span id="time', $show_id + 1, $program_times,;

it don't work, any idea?

1

I'm not sure I've understood the problem and the request.

it won't work when i have time more than 2 hours

So if you do:

$next_program_times = $program_times + 3;

It will give problems? Or you refer to multiple program_times?

when i try this:

$next_program_times = '<span id="time', $show_id + 1, $program_times,;

it don't work

I see few problems here:

  1. if you end the string with a comma you will generate a parsing error for unexpected ;
  2. you can use commas to separate variables and strings when outputting the variables and strings but not to assign them to a variable, so change the above to:

    $next_program_times = '<span id="time' . $show_id + 1 . $program_times;
    

    But if you add an integer to the string you loose the <span tag and end with something that looks like 110:00 AM. So something more correct is:

    $abc = $show_id + 1;
    $next_program_times = '<span id="time' . $abc . $program_times;
    

    This will generate something like <span id="time11410:00 AM which is still wrong, as an id cannot contain a space character.

  3. I don't know how you set $show_id but I assume this is set outside the loop, so there are good chances that it will generate a group of same ids at least as in your original code that does not include the $program_times variable. An id attribute in HTML must be unique over the document, if you don't fix it you can encounter problems with javascript functions or parsing libraries that look at the DOM.

But I suspect it wasn't this you were asking for, so what is the purpose of that string?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.