简体   繁体   English

格式化html表的日期字符串

[英]formatting date string for html table

Can someone show me how to change this date stamp and print this in an html table? 有人可以告诉我如何更改此日期戳并将其打印在html表中吗?

I have an input file with this time stamp format: 我有一个带有此时间戳格式的输入文件:

4-Start=20100901180002

This time format is stored like this in an array. 此时间格式像这样存储在数组中。 I print out the array like so to create an html table: 我像这样打印出数组,以创建一个html表:

foreach ($data as $row){
   $counter ++;                                        
   $class = $counter % 2 === 0 ? 'alt1' : 'alt2';       
   echo '<tr class="' . $class . '">';                  

     foreach ($keys as $column)                 
        if (isset($row[$column])){              
          echo '<td>' . $row[$column];
          } else {
          echo '<td>' . '' . '</td>';
        }
}
echo '</table>';

How do I change the timestamp in this table to this? 如何将此表中的时间戳更改为此? 2010-09-01 18:00:02

This is what you are looking for, http://de2.php.net/manual/en/function.strtotime.php 这就是您要寻找的东西, http://de2.php.net/manual/zh/function.strtotime.php

There is a similar question you can get more inputs from there as well.. How do I format the date and time That is received from database 还有一个类似的问题,您也可以从那里获得更多输入。. 如何格式化从数据库接收的日期和时间

EDIT: Yes you can use it an echo as well 编辑:是的,您也可以使用它作为回显

echo date("Y-m-d H:i:s",strtotime('20100901180002')) ; // 2010-09-01 18:00:02

You can even use CreateFromFormat as RC said, this is using CreateFromFormat in Procedural style. 您甚至可以按照RC的说明使用CreateFromFormat,这是以过程样式使用CreateFromFormat的。

 $date = date_create_from_format("YmdHis", '20100901180002');
 echo date_format($date, 'Y-m-d H:i:s');  // 2010-09-01 18:00:02

Refer to http://php.net/manual/en/datetime.createfromformat.php 请参阅http://php.net/manual/en/datetime.createfromformat.php

This part is strange, the elseif is never reached in my opinion. 这部分很奇怪,我认为从来没有达到过elseif

if (isset($row[$column])){
   echo '<td>' . $row[$column] . '</td>';
} elseif ($column == 'Condition') {
   echo '<td> Error </td>';
} else {
   echo '<td> </td>';
}

Regarding your format issue: 关于格式问题:

// PHP > 5.2
$date_s = "" . $row['Start'];
$date =  DateTime::createFromFormat("YmdHis", $date_s);
echo $date->format("Y-m-d H:i:s"); // 2010-09-01 18:00:02

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM