php exec 使用控制台和输入
例如,我想在控制台中更改电脑时间。
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:
*/
如何发送新时间?
Upd
没有 1 行命令 echo
#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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
time
控制台应用程序需要标准输入 (STDIN) 的输入,这是控制台应用程序的标准输入(因此对于任何其他标准命令行 (CLI) 应用程序也是如此,而不仅仅是时间):应该这样做,管道符号
|
将echo
的输出重定向为time
的输入。这是 Windows 和 UNIX 的共同原则。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):Should do the trick, the pipe symbol
|
redirects the output fromecho
as input totime
. That's a common principle for both windows and unix.