stanleybb Posted January 8, 2009 Share Posted January 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/ Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732466 Share on other sites More sharing options...
stanleybb Posted January 8, 2009 Author Share Posted January 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732469 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732477 Share on other sites More sharing options...
stanleybb Posted January 8, 2009 Author Share Posted January 8, 2009 Thanks that worked, the only other thing is that it displays time an hour ahead, how would that be fixed. Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732489 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 $result = @mysql_query("SELECT postTXT, postID, postTITLE, posterEMAIL, DATE_FORMAT(DATE_SUB(postTIME, INTERVAL 1 HOUR), '%h:%i %p | %m/%d/%Y') as postTIME FROM comtbl ORDER BY postID DESC"); Not sure if that will work but yea. Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732496 Share on other sites More sharing options...
stanleybb Posted January 8, 2009 Author Share Posted January 8, 2009 Thanks for your help. That worked a treat Quote Link to comment https://forums.phpfreaks.com/topic/140001-solved-timestamp/#findComment-732500 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.