Jump to content

datetime function display help


DarkJamie

Recommended Posts

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?

 

 

 

Link to comment
Share on other sites

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())";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.