DarkJamie Posted February 10, 2010 Share Posted February 10, 2010 I have written a personal blog site based on a tutorial for such and it works great, except for one thing. The tutorial was written to store the date of each post in the db and return the month, day and year. It works how it is supposed to, but I would like to display the time of each post as well. Here is where it gets messed up. I suspect the writer of the tutorial left out the time due to this very problem. The date is being set properly, but the time for each entry is the same, which obviously can't be. This is the code that the tutorial provided for the function: function formatDate($val) { $arr = explode("-", $val); return date("d M Y", mktime(0,0,0, $arr[1], $arr[2], $arr[0])); } Here is the change that I made for formatting: function formatDate($val) { $arr = explode("-", $val); return date("g:i A - F j, Y", mktime($arr[1], $arr[1], $arr[1], $arr[1], $arr[2], $arr[0])); } the function is called upon like this: echo formatDate($send->date); the mysql table was set like this: `date` datetime NOT NULL default '00-00-0000 00:00:00', I have changed the numbers in each instance of $arr in the above function to see how they would effect the results. Regardless of what values I put in, the times would change, but each entry would display the same time, rather than their actual post times. (all entries posted at 2:02 AM - February 10, 2010) I could just go back to the default setting to display only the day/month/year, but as I learn more PHP, I want to use the date and time values to display a calendar picture with sprites rather than a textual time stamp. So my question is, what would be the best way to properly display the timestamp for each entry? Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/ Share on other sites More sharing options...
schilly Posted February 10, 2010 Share Posted February 10, 2010 in your sql call do this: SELECT UNIX_TIMESTAMP(date_field) as date FROM table mysql will convert the timestamp field straight to a unix timestamp you can put in the date() fn so you can avoid all the formatting garbage. Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010453 Share on other sites More sharing options...
DarkJamie Posted February 11, 2010 Author Share Posted February 11, 2010 Thank you for your help in this. In order to be more clear with my naming convention, I have changed the "date" table name to "entry_date" in the db to avoid a conflict with the date function. Taking your recommendation, I've removed the included site_funct with it's date function and used UNIX_TIMESTAMP in my SQL call. Now I'm getting "0" (literally zero) rather than the entry date. No errors are generated My code is as follows: SQL table" `entry_date` datetime NOT NULL default '00-00-0000 00:00:00', My display PHP code: <? //Generate the query to display news content $query = "SELECT title, author, content AND UNIX_TIMESTAMP(entry_date) as date FROM news WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // get resultset as object $send = mysql_fetch_object($result); // print details if($send) { echo "<b class='style2'>$send->title</b> "; echo $send->date; echo "<br><i>Written by: </i><b>$send->author</b> "; echo "<br><br>"; echo nl2br($send->content); echo "<br><br><font size=2 color=red><a href=my_sent.php>Main menu.</a></font>"; } ?> Here is the SQL from the entry page: $query = "INSERT INTO news(id, title, content, author, entry_date) VALUES(0, '$title', '$content', '$author', NOW())"; Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010529 Share on other sites More sharing options...
schilly Posted February 11, 2010 Share Posted February 11, 2010 $query = "SELECT title, author, content, UNIX_TIMESTAMP(entry_date) as date FROM news WHERE id = '$id'"; remove the "AND" see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010539 Share on other sites More sharing options...
DarkJamie Posted February 11, 2010 Author Share Posted February 11, 2010 progress. Now I'm getting the unformatted UNIX timestamp, but when I changed the line to: echo $send->date("g:i A - F j, Y"); it returned Fatal error: Call to undefined function: date() in /r.php on line 204 so I'm not sure how to get the formatting right calling from the $send array. Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010543 Share on other sites More sharing options...
schilly Posted February 11, 2010 Share Posted February 11, 2010 i think you need to do this: echo date("g:i A - F j, Y", $send->date); Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010547 Share on other sites More sharing options...
DarkJamie Posted February 11, 2010 Author Share Posted February 11, 2010 Thank you so much. That was it. I tried so many variations trying to get it to display right. Now all I have to do is adjust time zone for the two hour difference and I'm done. Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010554 Share on other sites More sharing options...
DarkJamie Posted February 11, 2010 Author Share Posted February 11, 2010 Thanks again. Marking this one solved Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010563 Share on other sites More sharing options...
schilly Posted February 11, 2010 Share Posted February 11, 2010 -/+ 7200 on the $send->date for 2hr difference. Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010582 Share on other sites More sharing options...
DarkJamie Posted February 11, 2010 Author Share Posted February 11, 2010 exactly. I'm two hours ahead so +7200 seconds like so (for anyone wanting to know) echo date("g:i A - F j, Y", $send->date +7200); Quote Link to comment https://forums.phpfreaks.com/topic/191703-datetime-function-display-help/#findComment-1010584 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.