Jump to content

Help with date() and mktime() functions


bballer

Recommended Posts

New user of PHP here! I'm using it to display entries from my MYSQL database. Image below.

 

levelsk.png

 

This is the code snippet that displays information from the 'name', 'author' and 'date' columns of my database. I learned this technique from w3schools.

 

$result = mysql_query("SELECT * FROM levels ORDER BY date DESC");

while($row = mysql_fetch_array($result))
{
	echo "<tr>";
	echo "<td>" . $row['name'] . "</td>";
	echo "<td>" . $row['author'] . "</td>";
	echo "<td align='right'>" . $row['date'] . "</td>";
	echo "</tr>";
}

 

I would like to use the date() function to format the date differently. But the date() function requires that I convert my time into a timestamp. I know you have to use the mktime() function for this, but I don't know how.

 

Can someone show me how to pass the contents from $row[date] into the mktime() function?

Link to comment
Share on other sites

Much simpler to format it in the query using MySQL's native DATE_FORMAT() function, as long as the data is stored properly (which it is).

 

$result = mysql_query("SELECT `name`, `author`, DATE_FORMAT(`date`, '%b-%e-%Y') as `form_date` FROM `levels` ORDER BY `date` DESC");
while( $row = mysql_fetch_array($result) ) {
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['author'] . "</td>";
echo "<td align='right'>" . $row['form_date'] . "</td>";
echo "</tr>";
}

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.