开发者

formatting date string for html table

Can someone show me how to change this date stamp and print this in an html table?

I have an input file with this time stamp format:

4-Start=20100901180002

This time format is st开发者_开发知识库ored like this in an array. I print out the array like so to create an html table:

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

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.

 $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


This part is strange, the elseif is never reached in my opinion.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜