通过触摸功能设置文件日期修改

时间:2017-06-29 01:23:03

标签: php date time

我已阅读http://php.net/manual/en/function.touch.php有关如何设置文件修改日期的信息,但我的日期格式是:

$date = "5/11/2017 08:32 PM EST";

如何用该日期触摸文件?

$filename = "text.txt";

if (!touch($filename, $date)) {
    echo 'Whoops, something went wrong...';
} else {
    echo 'Touched file with success';
}

1 个答案:

答案 0 :(得分:4)

使用strtotime()将其转换为正确的格式:

$filename = "text.txt";

if (!touch($filename, strtotime($date))) {
echo 'Whoops, something went wrong...';
} else {
echo 'Touched file with success';
}

如果您100%确定格式,请使用DateTime::createFromFormat()以确保100%准确度(无论区域设置或区域):

$datetime = DateTime::createFromFormat("n/d/Y h:i A T", $date);

if (!touch($filename, $datetime->getTimestamp())) {
echo 'Whoops, something went wrong...';
} else {
echo 'Touched file with success';
}

您可以使用strtotime()DateTime::createFromFormat()看到here结果相同。