在php中,将时间戳转换为日期会给出错误的日期
我在从ftp上的文件夹中提取文件时,无法从时间戳中获取正确的日期在php中,将时间戳转换为日期会给出错误的日期,php,date,timestamp,Php,Date,Timestamp,我在从ftp上的文件夹中提取文件时,无法从时间戳中获取正确的日期 $it = new DirectoryIterator("blahblahblah/news"); $files = array(); foreach($it as $file) { if (!$it->isDot()) { $files[] = array($file->getMTime(), $file->getFilename()); }} rsort($files); foreach ($fi
$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>";
}
$it=newdirectoryIterator(“blahblahblah/news”);
$files=array();
foreach($it作为$file){
如果(!$it->isDot()){
$files[]=array($file->getMTime(),$file->getFilename());
}}
rsort(文件);
foreach($f文件){
$mil=$f[0];
$seconds=$mil/1000;
$seconds=round($seconds);
$theDate=日期(“d/m/Y”,美元秒);
回声“$theDate.”-“;
回声“
”;
}
我将按时间戳对文件进行排序,然后尝试用文件名和文件链接将它们回显出来。
问题是()的日期是1970年1月16日。。。我把时间戳放进了一个在线转换器,它们是准确的,所以我很困惑。
我还舍入了时间戳,但这也没有帮助。返回Unix时间戳
Unix时间戳通常是自Unix纪元以来的秒数(而不是毫秒数)
因此:$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>";
}
$it=newdirectoryIterator(“blahblahblah/news”);
$files=array();
foreach($it作为$file){
如果(!$it->isDot()){
$files[]=array($file->getMTime(),$file->getFilename());
}}
rsort(文件);
foreach($f文件){
$seconds=$f[0];
$seconds=round($seconds);
$theDate=日期(“d/m/Y”,美元秒);
回声“$theDate.”-“;
回声“
”;
}
返回Unix时间戳
Unix时间戳通常是自Unix纪元以来的秒数(而不是毫秒数)
因此:$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>";
}
$it=newdirectoryIterator(“blahblahblah/news”);
$files=array();
foreach($it作为$file){
如果(!$it->isDot()){
$files[]=array($file->getMTime(),$file->getFilename());
}}
rsort(文件);
foreach($f文件){
$seconds=$f[0];
$seconds=round($seconds);
$theDate=日期(“d/m/Y”,美元秒);
回声“$theDate.”-“;
回声“
”;
}