Jump to content

[SOLVED] TimeStamp


stanleybb

Recommended Posts

Hiya everybody

 

The problem is that i have the comment thing working so that people can leave comments but the time displays wrong. For example 08: 1 AM | 1-/-0/09 why is this. But on the database the time is correct. Please help

 

 

<?

$dbcnx = mysql_connect("localhost", "88888", "888888");

mysql_select_db("888888");

 

     

    $result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");

    if (!$result) {

      echo("<b>Error performing query: " . mysql_error() . "</b>");

      exit();

    }

   

    while ($row = mysql_fetch_array($result) ) {

$msgTxt = $row["postTXT"];

$msgId = $row["postID"];

$SigName = $row["posterNAME"];

$SigDate = $row["postTIME"];

$msgTitle = $row["postTITLE"];

$url = $row["posterEMAIL"];

 

$yr = substr($SigDate, 2, 2);

$mo = substr($SigDate, 4, 2);

$da = substr($SigDate, 6, 2);

$hr = substr($SigDate, 8, 2);

$min = substr($SigDate, 10, 2);

if ($hr > "11") {

$x = "12";

$timetype = "PM";

$hr = $hr - 12;

}else{

$timetype = "AM";

}

if (!$url) {

$url = "#";

}else{

$stat = $url;

$url = "mailto:" . $url . "";

}

 

echo("<p><b>$msgTitle</b> $msgTxt<br><div align=right>

  $hr:$min $timetype | $da/$mo/$yr | $msgId, <a href='$url'>$SigName</a></div></p>");

    }

 

 

?>

 

 

 

This is all the code to my timestamp problem

Link to comment
https://forums.phpfreaks.com/topic/140001-solved-timestamp/
Share on other sites

Why not use the date function to get the time displayed how you want it, instead of doing your weird logic? If the timestamp, isn't really a timestamp, you can do strtotime or in your mysql query use the DATE_FORMAT function. (Preferably the DATE_FUNCTION)

 

This:

$result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");

 

Becomes:

$result = mysql_query("SELECT postTXT, postID, postTITLE, posterEMAIL, DATE_FORMAT(postTIME, '%h:%i %p | %m/%d/%Y') as postTIME FROM comtbl ORDER BY postID DESC") or die(mysql_error()); 

 

I am unsure if that is proper syntax, but yea that would reduce your PHP code just by getting the date out of the DB in the correct format =)

Link to comment
https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732466
Share on other sites

Sorry what i mean is that everything displays fine in the right order but the time displays like this: 08: 1 AM | 1-/-0/09 all the time and it makes no sense

 

Right, did you try the above? It is not very good practice to do your own time formatting when you have so many available resources...

 

<?php
$dbcnx = mysql_connect("localhost", "88888", "888888");
mysql_select_db("888888");

     
    $result = @mysql_query("SELECT postTXT, postID, postTITLE, posterEMAIL, DATE_FORMAT(postTIME, '%h:%i %p | %m/%d/%Y') as postTIME FROM comtbl ORDER BY postID DESC"); 
    if (!$result) {
      echo("<b>Error performing query: " . mysql_error() . "</b>");
      exit();
    }
   
    while ($row = mysql_fetch_array($result) ) {
   $msgTxt = $row["postTXT"];
   $msgId = $row["postID"];
   $SigName = $row["posterNAME"];
   $SigDate = $row["postTIME"];
   $msgTitle = $row["postTITLE"];
   $url = $row["posterEMAIL"];
   
   /* All of this is useless code now, no need for it.
   $yr = substr($SigDate, 2, 2);
   $mo = substr($SigDate, 4, 2);
   $da = substr($SigDate, 6, 2);
   $hr = substr($SigDate, 8, 2);
   $min = substr($SigDate, 10, 2);
   if ($hr > "11") {
   $x = "12";
   $timetype = "PM";
   $hr = $hr - 12;
   }else{
   $timetype = "AM";
   }*/
   if (!$url) {
   $url = "#";
   }else{
   $stat = $url;
   $url = "mailto:" . $url . "";
   }

echo("<p><b>$msgTitle</b> $msgTxt<br><div align=right>
   $SigDate | $msgId, <a href='$url'>$SigName</a></div></p>");
    }
?>

 

Is that not a ton simpler...? And, if my SQL is bad, all you have to do is follow the errors to correct it, but I think it should be good, the only thing I wonder about is the " | " symol, not sure if that is kosher.

 

Give it a try and see what happens.

Link to comment
https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732477
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.