0

I can't get the string to read out what I want in the final else statement. I want {$start} to read out what the start is set to in the if statement.

I've tried {$start}, .$start, %d.

if ( $time < $start ) {

    $time_until_opens = ( $start->getTimestamp() - $time->getTimestamp() ) / 60;


    if ( $time_until_opens <= 5 ) {
        $this->message = __( 'Happy Hour Starts Soon', 'my-listing' );
        $this->status = 'Happy Hour Soon';

        return true;
    } elseif ( $time_until_opens <= 30 ) {
        $this->message = sprintf( __( 'Happy Hour In %d Minutes', 'my-listing' ), ( round( $time_until_opens / 5 ) * 5 ) );
        $this->status = 'opening';

        return true;
    } else {
        $this->status = 'closed';
        $this->message = __( 'Happy Hour At {$start}', 'my-listing' );
    }
}

$start is set to 4:00 PM. It doesn't show 4:00 PM. It shows {$start}.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – miken32 Apr 22 '19 at 23:37
  • Single quotes don't parse variables. – miken32 Apr 22 '19 at 23:38
  • You are misusing the `__` function though, you should be using `sprintf` as you did with the message above. You can't create a translation string with a variable in it. – miken32 Apr 22 '19 at 23:39

2 Answers2

0

The problem on your code it's that you're not using the __() function correctly. To solve this issue you should use instead sprintf and bind the variable you want to place on the string.

$this->message = sprintf(__('Happy Hour At %s', 'my-listing'), $start);

%s will get replaced with the $start string. Note that $start must be a string, otherwise it won't print as you expect.

Greetings, Matt

ionoproxy
  • 24
  • 2
0

You need to use double qoutes not a single qoute to read as a variable. for example

'Happy Hour At {$start}' change to "Happy Hour At $start".

rrsantos
  • 404
  • 4
  • 14