search db by date

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
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

search db by date

Post by ddragas »

$datum = time();

All entries in db in field 'date' are inserted with value of $datum at the time of inserting into db

Is folowing code correct to search db in column 'date' where 'date' = '21.10.2005'

Code: Select all

$datum = $_POST['pojam'];

				 include("../stranice/con_db.php");

				$result = mysql_query("select * from $table_name where 
				date = date("d.m.Y ",  $datum)") or
				die (mysql_error());
	
					while($row = mysql_fetch_array($result))
						{

							etc......
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: search db by date

Post by foobar »

You have a syntax error there, and a semantic error.

This:

Code: Select all

mysql_query("select * from $table_name where date = date("d.m.Y ",  $datum)")
should be:

Code: Select all

mysql_query("select * from $table_name where date = '".date("d.m.Y ",  $datum)."'")
Assuming your date field is of type text, blob or varchar.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

THANK YOU
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I don't understand why people don't just use timestamps in databases... ??
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The Ninja Space Goat wrote:I don't understand why people don't just use timestamps in databases... ??
Or the databases own DATE field type - makes calculations and formatting of dates much easier.

Mac
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

TIMESTAMP really isn't a unix timestamp.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

DATE or DATETIME fields are the way to go...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

the only advantage date/datetime has over a timestamp in my opinion is that they are human-readable if you are looking at them in phpmyadmin or something.
Post Reply