formatting date string for html table - php

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

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

Related

Calculeted time adding together in php (H:i:s)

I have a query which returns me some data.
//I loop through the query's result and write all data out
while($row =$result->fetch_assoc()){
echo $row['place'] . '|';
echo $row['gets_busy'] . '|';
echo $row['gets_empty '] . '|';
echo $row['time_between'] . '|';
echo "<br>";
}
ROOM01|2021-03-05 12:02:56|2021-03-05 12:04:02|00:01:06|
ROOM01|2021-03-05 12:05:42|2021-03-05 12:07:48|00:02:06|
ROOM01|2021-03-05 12:07:48|2021-03-05 12:12:54|00:05:06|
ROOM02|2021-03-05 12:15:54|2021-03-05 12:17:00|00:01:06|
ROOM02|2021-03-05 12:17:01|2021-03-05 12:23:17|00:05:16|
ROOM02|2021-03-05 12:23:59|2021-03-05 12:25:45|00:01:46|
I want something to write for the user like:
ROOM01 was busy for -> 00:08:18 during 2021-03-05
ROOM02 was busy for -> 00:08:08 during 2021-03-05
Can this be accomplished by PHP?
You can group into another array, by using the places and date as keys.
while($row = $result->fetch_assoc())
{
$place = $row['place']; // shortcut
$start = strtotime($row['gets_busy']); // start timestamp
$stop = strtotime($row['gets_empty']); // end timestamp
$time = $stop - $start; // duration in seconds
$date = date('Y-m-d', $start); // date for grouping
// group by place and date
if (!isset($data[$place][$date])) {
$data[$place][$date] = 0; // init with 0
}
$data[$place][$date] += $time; // add time.
}
// now, display data :
foreach ($data as $place => $dates) {
echo $place . ':<br>';
foreach ($dates as $date => $duration) {
$interval = new DateInterval('PT'.$duration.'S');
echo ' -> ' . $date . ' : ' . $interval->format('%h:%i:%s') . '<br>';
}
}

Select value inside a dropdown using ternary operator inside a foreach php loop

How do you select a dropdown value inside a foreach loop. The code below generate time with 30 minutes gap. I cannot able to select the value from mysql database.
$sql = "SELECT * FROM batches WHERE batch_id = '$id'";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
// Name
echo "<label>Batch name</label> ";
echo "<input type=text name=name value='".$row['name']."' required> <br />";
// Start time
echo "<label>Start time</label> ";
echo "<select name=starttime value=''> Start time </option>";
$range = range( strtotime("05:00"), strtotime("21:00"), 30 * 60);
foreach($range as $time){
$foo = date("H:i", $time) . "</br>";
$selected = ($foo == $row['start_time']) ? 'selected="selected"' : '';
echo "<option value='$foo'" . $selected . ">" . $foo . "</option>";
} // foreach
echo "</select>";
} // while
Please help!!
You are concatenating a break line (<br>) to your $foo variable, which is why the comparison fails. You don't need this break line inside a option tag either, so just get rid of it:
foreach($range as $time){
$foo = date("H:i", $time);
$selected = ($foo == $row['start_time']) ? 'selected="selected"' : '';
echo "<option value='$foo'" . $selected . ">" . $foo . "</option>";
} // foreach
Also, by your comments, the start_time coming from the database contains seconds as well, so you need to format your timestamps accordingly:
$foo = date("H:i:s", $time);

conditional formatting html table in php with time stamp comparison

echo '<table style="width:100%"> <tr>';
echo '<td>Order</td>';
echo '<td>Destination</td>';
echo '<td>Location</td>';
echo '<td>Status</td>';
echo '<td>TimeStamp</td>';
echo '</tr>';
if($result) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
echo '</table>';
}
I want to change the background of the row turned a different color is the time stamp is more than 60 minutes past the current time. any help would be much appreciated. i dont even know where to begin.
Thanks
edit: format of my time stamp "2015-07-17 19:17:31"
Do an if to see if time is over 60 minutes and if so assign it a class with a different background color. Since you didn't clarify I'm going to assume you are using unix timestamp time().
$currTime = time();
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$calc = $currTime - $row['TimeStamp'];
if($calc > 3600){
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
}
echo '<tr class="'.$rowClass.'"><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
Then add CSS to define the two classes
.oldOrder{
background-color: #ccc;
}
.normalOrder{
background-color: #fff;
}
$NowDT = date("Y-m-d H:i:s");
$NowRDT = strtotime($NowDT);
$DateTimeNF = $row['TimeStamp'];
$TimeRF = strtotime($DateTimeNF);
$TimeDifference = $NowRDT - $TimeRF;
$TimeDifferenceHours = $TimeDifference/3600;
If ($TimeDifferenceHours > "1")
{
echo '<tr bgcolor="#E60000"><td>';
}
else
{
echo '<tr><td>';
}
It seems like your timestamp is a String, you need to convert your string to a Unix TimeStamp using strtotime:
$rowTime=strtotime($row['TimeStamp']);
and then substract it from the actual time, as the previous answer, to obtain the difference in seconnds. Divide it by 60 and you get it on minutes.
$currenTime=time();
$diffSeconds=$currenTime-$rowTime;
$diffMinutes=$diffSeconds/60;
and then check if the difference is bigger than 60:
$myCSS="normalRow";
if ($diffMinutes>60) { $myCSS="differentRow"; }
and then use that CSS Style ($myCSS) in your table row:
echo '<tr class="'.$myCSS.'"><td>';
You need to define those two styles in your CSS file or your HTML Header like:
<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>
And that's it. Hope it helps.

sql query fetch error [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 3 years ago.
I'v got some code but keep getting error's also I'm new to php so any help would be great :)
Here's my code
<?php
// Create connection
$con = mysqli_connect("host","database","pswd");
// Database Connection Check
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
break;
}
echo "<table>";
echo nextweek("heren1kalender","Heren 1e Provinciale");
echo "</table>";
function nextweek($table, $ploegNaam) {
// Get this weeks dates (monday and sunday and month)
$current_day = date("N")
$days_to_sunday = 7 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("d", strtotime("- {$days_from_monday} Days"));
$sunday = date("d", strtotime("+ {$days_to_sunday} Days"));
$month = date("m", strtotime("+ {$days_to_sunday} Days"));
// SQL query
$result = mysqli_query($con,"SELECT datum, hoofd, thuisploeg, bezoekers FROM " . $table . " WHERE thuisploeg LIKE '%Lochristi%' OR bezoekers LIKE '%Lochristi%'");
while($row = mysqli_fetch_array($result)) {
$string ="";
// Get day and month from array
$dag = substr($row['datum'], 4, -6);
$maand = substr($row['datum'], 7, -3);
if ($dag >= $monday AND $dag <= $sunday AND $maand == $month) {
if (strpos($row['thuisploeg'],'Lochristi') !== false) {
$string .= "<tr>";
if (substr($row['hoofd'], 0, 1) >= 3) {
$string .= '<td class="win">' . $row['hoofd'] . "</td>";
}
else {
$string .= '<td class="loss">' . $row['hoofd'] . "</td>";
}
$string .= '<td>' . $ploegNaam . '</td>';
$string .= "<td><strong>VS</strong></td>";
$string .= '<td>' . $row['bezoekers'] . '</td>';
}
elseif (strpos($row['bezoekers'],'Lochristi') !== false) {
$string .= "<tr>";
if (substr($row['hoofd'], 0, 1) >= 3) {
$string .= '<td class="loss">' . $row['hoofd'] . "</td>";
}
else {
$string .= '<td class="win">' . $row['hoofd'] . "</td>";
}
$string .= '<td>' . $row['thuisploeg'] . '</td>';
$string .= "<td><strong>VS</strong></td>";
$string .= '<td>' . $ploegNaam . '</td>';
}
$string .= "</tr>";
}
}
return $string;
}
?>
And these are the PHP error's I'm getting:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/a2902119/public_html/test.php on line 24
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/a2902119/public_html/test.php on line 26
Thanks for the help!
After fixing the other issues: Variable scope. $con is not available in the functions. Pass it in as an arg or rework into classes or something.
Your SQL connection doesn't seem correct.
It should have 4 parts.
$con=mysqli_connect(hostaddress,dbuser,dbpass,databasename);

php - printing keys with embedded function

I need help figuring out these php print (echo) statements and where to place them. I have an embedded function 'strotime' that is transforming time (column 'StartTime') to a format, but I cannot get it to print out correctly. No errors, just no changes or use of the function.
Can someone help me figure out where to place this properly in this foreach loop?
(as you can see, i placed at beginning and tried an if statment too..but no luck). Thanks for
your help.
$keys = array('Server', 'Target','Logdate','Set','StartTime', 'Length','Size','Status');
echo '<table><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($keys as $column)
//if ($column == 'StartTime') {
// echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column =='StartTime') {
echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
} elseif ($column == 'Status') {
echo '<td> Check for Errors </td>';
} else {
echo '<td> </td>';
}
//}
}
echo '</table>';
In the beginning if foreach ($data as $row){ loop, do this:
$row['StartTime'] = date("Y-m-d H:i:s",strtotime($row['StartTime']));
And then display it like any other column.
Change
if (isset($row[$column])) {
to
if (isset($row[$column]) && $column != "StartTime") {
and by the way: you're missing the </tr> tags.

Categories