Date conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Javrixx
Forum Commoner
Posts: 32
Joined: Thu Aug 24, 2006 2:05 pm

Post by Javrixx »

Everah wrote:Ok, how are your dates stored in your database?
Copy and paste from the phpAdmin:

Edit Delete Test File Name 2006-10-04 1pa542iqzn92oepaz test 1
Edit Delete Steve's Survey 2005-10-03 1pa542iqzn92oepaz stevesurvey 1
Edit Delete test17 2006-10-04 17iz831nqbe84pqinz test17 17
Edit Delete filename 2006-10-10 encryptedfoldername encryptedfilename 1
Edit Delete filename2 2006-10-10 foldername filename 1
Edit Delete anotherfilename 2006-02-08 folder file 1



So it's 2006-12-25. Year-Month-Day
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

As per volka's post (notice that the formatted date is aliased to a field named 'datef', so that is what you reference in the while loop, not the field `date`):

Code: Select all

<?php
{
	// list all users
	$sql = "SELECT DATE_FORMAT(`date_field`, '%W %M %Y') as datef, filename, folder, file
		FROM `table_name`
		WHERE `user_id` = $userid
		ORDER BY `date_field` LIMIT $start, $limit";
}

if ($result = mysql_query($sql)) {
	echo $row['datef'] . "<br />\n";
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

and to reverse the order add DESC to the statement.
ORDER BY `date_field` DESC


ORDER BY fieldA ASC (ascending, default)
ORDER BY fieldA DESC (descending)
also works for more than one field: ORDER BY fieldA ASC, fieldB DESC
Javrixx
Forum Commoner
Posts: 32
Joined: Thu Aug 24, 2006 2:05 pm

Post by Javrixx »

Awesome, everything works exactly as needed. Thank you so much guys for the help, I really appreciate it. I just have one last thing to figure out until I'm almost finished with this project (please see my other post).
Post Reply