2017-06-29 44 views
0

我已經閱讀如何設置文件的修改日期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'; 
} 
+0

'strtotime'可以將日期字符串轉換爲Unix時間戳 –

+0

touch()需要第二個參數爲UNIX時間戳。 https://secure.php.net/manual/en/function.strtotime.php這是一種做法,但我建議你使用DateTime方法來確保最好的結果 –

+2

Btw;你還有其他問題需要解決,最好將它們標記爲已解決。他們被認爲仍然沒有解決/開放。 –

回答

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'; 
} 

你可以看到here結果是使用strtotime()DateTime::createFromFormat()相同。

+0

最好檢查OP的以前的問題;-)我給他們一個[輕微友好微調](https://stackoverflow.com/questions/44814911/set-file-date-modification-via-touch-function#comment76610488_44814911) –

+0

@ Fred-ii-從檢查OP的以前的問題中得到什麼?我應該做一些不同的事情嗎? – FrankerZ

+0

好吧,很多時候,人們會提出很多問題,並得到很多解決方案,但不要將它們標記爲已解決。他們張貼,他們得到解決方案並跑掉。不知道你是否會在這裏看到一個綠色的勾號,但我可能是錯的。 –

相關問題