views:

52

answers:

2

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 stored 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

+4  A: 

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.. http://stackoverflow.com/questions/3632747/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

lucky
can i put this function within an echo statement to print out dynamically?
jda6one9
Answer is edited.
lucky
ok, thanks lucky. I'm just confused where to embed this with using the `<td>` tags.
jda6one9
You can keep where your data is getting printed. For example i assume this is how you are printing data echo '<td>' . $row[$column]; echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) ;
lucky
+1  A: 

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
RC
thanks RC. I cleaned up that condition (see my edit). As for the formatting, would I embed `$date_s` within the `<td>` tags, like I did in my foreach? thanks again.
jda6one9
No you need to use `$date->format(...)` in your TD. (note: a `</td>` is now missing in your question)
RC