php exec 使用控制台和输入

发布于 2025-01-06 00:08:08 字数 711 浏览 3 评论 0原文

例如,我想在控制台中更改电脑时间。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2025-01-13 00:08:08

time 控制台应用程序需要标准输入 (STDIN) 的输入,这是控制台应用程序的标准输入(因此对于任何其他标准命令行 (CLI) 应用程序也是如此,而不仅仅是时间):

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

应该这样做,管道符号 |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):

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文