Displaying date in php

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

Post Reply
aniltc
Forum Newbie
Posts: 17
Joined: Thu Nov 16, 2006 6:42 am

Displaying date in php

Post by aniltc »

hi all


I am new to php. I have a problem in my site.I have a news and Events section in my home page,in which i want to display the latest news and events based on current server date.I am storing entire date in the database.

database structure

Input to Mysql database

DATE(dd-mm-yyyy) News and EVENTS
8-10-2005 XXXXXXXXXX
8-10-2006 XXXXXXXXXX
1-11-2006 XXXXXXXXXX
14-11-2006 XXXXXXXXXX
16-11-2006 XXXXXXXXXX
18-11-2006 XXXXXXXXXX
30-12-2006 XXXXXXXXXX

#########################################################
I need the o/p in the following format(consider 16-11-2006 is the current Server date)

DATE(dd-mm-yyyy) News and EVENTS
16-11-2006 XXXXXXXXXX
18-11-2006 XXXXXXXXXX
30-12-2006 XXXXXXXXXX
14-11-2006 XXXXXXXXXX
01-11-2006 XXXXXXXXXX
8-10-2006 XXXXXXXXXX
8-10-2005 XXXXXXXXXX



please help me the logic or providing the code >:(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you using a native DATE(TIME) field or a string type? Your post would suggest a string type, which in the format you've chosen more annoying to sort. If it were a DATE(TIME) type, all you'd require is an ORDER BY clause in your query, and probably a LIMIT clause too.
aniltc
Forum Newbie
Posts: 17
Joined: Thu Nov 16, 2006 6:42 am

Post by aniltc »

hi

Thanks for ur reply

I am not looking for ascending or descending order.if the cuurent server date matches with any of the news and events which i have mentioned ,then that particular date should dispaly first otherwise it should display the latest date; please help me
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

(
	SELECT
		...
	WHERE
		datefield=Curdate()
)
UNION
(
	SELECT
		...
	WHERE
		datefield!=Curdate()
	ORDER BY
		datefield DESC
)
Curdate() is mysql specific, you may have to replace it.

edit: forgot parentheses
Last edited by volka on Thu Nov 16, 2006 9:53 am, edited 2 times in total.
aniltc
Forum Newbie
Posts: 17
Joined: Thu Nov 16, 2006 6:42 am

date display in php

Post by aniltc »

hi

thanks for ur reply

let me check it and come back soon


:)
Post Reply