1

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);

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Does that work in windows? (the "c:\Users" part suggests that OS)
@cetver mhh afaik does php not support stdin? I had the same problem with SSH long time ago... you cannot just put in the password... i think its the same problem here
@cetver the php://stdin is used to get stuff from the user over the cli. if you want to put in to a shell command from php programmaticaly you have to use a pipe.
Alternative open the process with file-pointers to STDIN/STDOUT/STDERR: proc_open - more differentiated but more control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.