2012-09-11 2 views
0

ftp의 폴더에서 파일을 가져 오는 동안 내 타임 스탬프에서 정확한 날짜를 가져 오는 데 문제가 있습니다.날짜를 타임 스탬프로 변환하면 PHP에서 잘못된 날짜가 표시됩니다.

$it = new DirectoryIterator("blahblahblah/news"); 
$files = array(); 
foreach($it as $file) { 
if (!$it->isDot()) { 
    $files[] = array($file->getMTime(), $file->getFilename()); 
}} 

rsort($files); 
foreach ($files as $f) { 
    $mil = $f[0]; 
    $seconds = $mil/1000; 
    $seconds = round($seconds); 
    $theDate = date("d/m/Y", $seconds); 
echo "<img src=\"images/content/social-icons/article.png\" width=\"18\" height=\"19\" alt=\"article\">" . $theDate . "- <a style=\"background-color:transparent;\" href=\"news/$f[1]\">" . $f[1] . "</a>"; 
echo "<br>"; 

} 

파일을 타임 스탬프로 정렬 한 다음 파일 이름과 파일에 대한 링크를 표시합니다. 문제는 date()가 1970 년 1 월 16 일에 나왔다는 것입니다 ... 타임 스탬프를 온라인 변환기에 넣었고 정확하기 때문에 혼란 스럽습니다. 나는 또한 타임 스탬프를 반올림했지만 도움이되지 않습니다.

답변

3

getMTime은 유닉스 타임 스탬프를 반환합니다.

유닉스 타임 스탬프는 일반적으로 Unix Epoch 이후의 초 수입니다 (밀리 초 수가 아님). See here.

이렇게 : $seconds = $mil/1000;가 오류의 원인입니다.

간단히 $seconds = $f[0]으로 설정하면 도움이됩니다.

수정 코드 :

$it = new DirectoryIterator("blahblahblah/news"); 
$files = array(); 
foreach($it as $file) { 
if (!$it->isDot()) { 
    $files[] = array($file->getMTime(), $file->getFilename()); 
}} 

rsort($files); 
foreach ($files as $f) { 
    $seconds = $f[0]; 
    $seconds = round($seconds); 
    $theDate = date("d/m/Y", $seconds); 
echo "<img src=\"images/content/social-icons/article.png\" width=\"18\" height=\"19\" alt=\"article\">" . $theDate . "- <a style=\"background-color:transparent;\" href=\"news/$f[1]\">" . $f[1] . "</a>"; 
echo "<br>"; 

} 
+0

챔피언! 정말 고마워! – user482024

관련 문제