XY BLOG

XY的BLOG

PHP修改Linux系统时间

Published on in Posts
Updated on
Short link: https://b.xyzt07.cn/s/d
Share this post
A ⇄ 文
⚠️ This entry is already over one year old. It may no longer be up to date. Opinions may have changed.

linux普通用户没有权限使用date -s修改时间

date -s “2023-06-01 00:00:00”

解决方法:

sudo chmod 4755 /bin/date

附上PHP修改系统时间代码

<?php
if ($_POST['time']) {

    $st = date('Y-m-d H:i:s', strtotime($_POST['time']));

    $cmd = sprintf('date -s "%s"', $st);

    system($cmd);
    header("Location: /settime.php");
} else {
?>

    <form action="" method="post">
        <input type="datetime-local" name="time" value="<?php echo date("Y-m-d\TH:i:s", time()) ?>" />
        <input type="submit" value="确定" />
    </form>
<?php
}
?>