cetver
cetver

Reputation: 11829

php exec working with console and input

For example, I want change PC time in console.

C:\Users\user>time
The current time is: 15:25:21,04
Enter the new time:

PHP:

exec('time', $out, $ret);
/*
$out = 
The current time is: 15:25:21,04
Enter the new time:
*/

How to send new time ?

Upd

Without 1 line commands echo <timestring> | time

#Thx to reox
#Working example:
$next_hour = date('H:i:s', strtotime('+1 hour'));
$command = 'time';
$handle = popen($command, 'r');
echo 'Command responce: <br>';
while($line = fgets($handle)) {
    echo $line;
    echo '<br>';
}
pclose($handle);
$handle = popen($command, 'w');
fputs($handle, $next_hour);
pclose($handle);

Upvotes: 1

Views: 1864

Answers (1)

reox
reox

Reputation: 5217

The time console application expects input at standard input (STDIN), that's the standard input for console applications (so this is true for any other standard commandline (CLI) application as well, not only time):

exec('echo <timestring> | time', $out, $ret);

Should do the trick, the pipe symbol |redirects the output from echo as input to time. That's a common principle for both windows and unix.

Upvotes: 1

Related Questions