You can use ffprobe to actually get a json string. You can then convert this json string to an object in php with json_decode. This is more elegant and simplier.
Code:
exec('ffprobe -v quiet -loglevel quiet -show_format -show_streams -print_format json file.mp4', $output);
$json = json_decode(implode('', $output));
$duration = $json->format->duration;
Now in your watermark cmd you replace the duration with the above $duration variable.