954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

DATE problem

I Need to insert a date into mysql database i dont know which is better
using DATE or VARCHAR as the date field ??
im using now the DATE type but whenever i insert any date its not inserted in the DB
0000-00-00 it appears like this .

the Code:

$mydate=date("$year.$month.$day");

insert into ....values ($mydate);

i dont know what's wrong

plz help

yasmena
Light Poster
45 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

I Need to insert a date into mysql database i dont know which is better using DATE or VARCHAR as the date field ?? im using now the DATE type but whenever i insert any date its not inserted in the DB 0000-00-00 it appears like this .

the Code:

$mydate=date("$year.$month.$day");

insert into ....values ($mydate); i dont know what's wrong

plz help


The dashes are required.

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

Replace dots with dashes. Try below.

$mydate=date("$year-$month-$day");
PoA
Posting Whiz in Training
237 posts since Jul 2004
Reputation Points: 26
Solved Threads: 9
 

what error you got?????

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

I think there is problem in inserting the values into database can you once check the syntax of insert query or if you provide the query i will look into it

srilakshmitr7
Light Poster
32 posts since Aug 2008
Reputation Points: 16
Solved Threads: 2
 

Regardless of what the error is I find it best to store a unix timestamp in the database in a "int" field and then use PHP to manipulate it.

This produces a very fast db table when sorting by date.

This is the best practice until you become more familiar with php and mysql and explore the differences in formatting the date.

To get the unix timestamp do something like:

$unix_time = date('U');

you can then format this by doing:

$formatted_time = date('Y-m-d',$unix_time);

$formatted_time_plus_1_week = date('Y-m-d',strtotime("+1 week",$unix_time));

Very simple.

kevindougans
Junior Poster
129 posts since Oct 2007
Reputation Points: 13
Solved Threads: 7
 

U know Thanks So much i solved the problem
but now i faced another
when the date is displayed its displayed YYYY-MM-DD
is their anyother way to display it as DD-MM-YYYY ?????????????????
if im storing as DATE

yasmena
Light Poster
45 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 
$date = '2008-09-29';
$newDate = date('d-m-Y', strtotime($date));
PoA
Posting Whiz in Training
237 posts since Jul 2004
Reputation Points: 26
Solved Threads: 9
 

Thank You Very Much POA
but may i ask why are u using
strtotime here ?? why not just say
$newDate=date('d-m-Y');

yasmena
Light Poster
45 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 
Thank You Very Much POA but may i ask why are u using strtotime here ?? why not just say $newDate=date('d-m-Y');


That would be fine if you just wanted today's date.

R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You