Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying 2 different things that I don`t get to work.

First, I am trying divide $dt (epoch time 13 characters) to divide by 1000 so it shows the correct date and time since it is saved in the MySQL database in miliseconds. I tried to divide it with /1000 on different places in the PHP, and in the MySQL query with different examples from the internet, but with everything I try I get a different error.

Second, I want the output of BackupState to be an image. For example, $row['BackupState'] shows STATE_SUCCESS, STATE_SUCCCES_WITH_WARNINGS, STATE_BUSY or STATE_NOT_COMPLETED. For every state I would like to show a different image.

C#
<?php 
$conn = new mysqli('host', 'user', '', 'database');
if ($conn->connect_error) {
	die("Connection error: " . $conn->connect_error);
}

$result = $conn->query("SELECT * FROM backupstate_tbl ORDER BY BackupTime DESC LIMIT 20");
if ($result->num_rows > 0) {
  // output the opening table tag only once before the loop
  echo '<table class="table" align="center">';
  echo "<tr><th>State</th><th>Name</th><th>Description:</th><th>Gestart:</th></tr>";
  while ($row = $result->fetch_assoc()) {
    			$epoch = $row['StartTime']; 	//Start Time of backup
    			$dt = new DateTime("@$epoch"); 				//Output date has to be divided by 1000
    			
    echo "<tr><td>"; 
    echo $row['BackupState'];
    echo "</td><td>";
	echo $row['ClientName'];
    echo "</td><td>";     
    echo $row['BackupTime'];
    echo "</td><td>";    
    echo $dt->format('Y-m-d H:i:s');
    echo "</td></tr>";
}
  // output the closing table tag only once at the end of loop
  echo "</table>";
}
?>


What I have tried:

I tried to complete this with different examples from the internet, but get an error with everything I try.
Posted
Updated 8-Mar-16 2:21am
Comments
CHill60 8-Mar-16 6:08am    
What are the errors you get?
Osseknar 8-Mar-16 6:25am    
If I try to divide the $dt by 1000 in the query I get Notice: Trying to get property of non-object in

When I make a $dt2 and try to divide $dt like this: $dt2 = $dt / 1000;
I get an error: Notice: Object of class DateTime could not be converted to int in

Or like this: $dt2 = ("$dt / 1000");
I get an error:
Catchable fatal error: Object of class DateTime could not be converted to string in

Honestly I have no idea what i`m doing, as you probably can see:)
CHill60 8-Mar-16 6:34am    
"as you probably can see" ... actually I don't know any PHP - I posted the comment as I know those that do would need that extra bit of detail before they could help :)
Sinisa Hajnal 9-Mar-16 2:13am    
I don't know PHP either, but the errors tell you, in order: you're trying to get the value of something that is not an object (probably not set); second one tells you that you're dividing date with integer and the system cannot convert $dt2 to integer; finally, $dt is DateTime and the system cannot convert it to string.

There is probably some method or property to get milliseconds out of the date...at the very least there will be DateDiff method of some kind where you can find out how many milliseconds from some arbitrary starting date.
Osseknar 9-Mar-16 6:35am    
When I add the milliseconds, it still shows the wrong date. I solved it buy maken a substring that only gets the first 10 characters from the row:

$epoch = $row['StartTime']; //Start Time of backup
$epoch10 = substr($epoch,0,10); // Get first 10 chars
$dt = new DateTime("@$epoch10");
echo $dt->format('Y-m-d H:i:s')

Thanks for your input Sinisa!

1 solution

The part with the images I figured out. I did it like this:

echo "<tr><td>";
            if($BackupState == 'BS_STOP_SUCCESS')
            {
            echo '<img src="images\apply.png" width="16" height="16" alt=\"Success\">';
            }
            elseif($BackupState == 'BS_STOP_SUCCESS_WITH_ERROR')
            {
            echo '<img src="images\warning.png" width="16" height="16" alt=\"Warning\">';
            }
            else
            {
            echo '<img src="images\cancel.png" width="16" height="16" alt=\"Cancelled\">';
            } ?>
 
Share this answer
 
Comments
Sinisa Hajnal 9-Mar-16 2:13am    
You could name the images like the states and avoid all ifs just putting src="$Backupstate.png" :)
Osseknar 9-Mar-16 6:38am    
That is a great idea, i`m gonna try that as soon as I have time. Thanks again for your input! Much appriciated!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900