DATE

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Niko
Forum Newbie
Posts: 9
Joined: Tue Apr 20, 2004 6:17 am

DATE

Post by Niko »

What is the best way to insert a date field into a mysql database through PHP,

ive tried DATE()

and <?php echo date("j/n/y h:i a");?>

however sometimes these dont work right.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ok well insert time into the DB
їphp]
$number_time = time();
// run insert
ї/php]
then to format it in a page when returning the record
їphp]
$formatted_time = date ("g:ia - M j", $number_time);
ї/php]

would output the following
їcode]11:00am - May 1ї/code]

hope that helps
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You should have this in mind, delaing with the timestamp type in the database:

Code: Select all

echo time()."<br />";
    $result = mysql_fetch_assoc(mysql_query("select NOW() as timetest"));
    print_r($result);
    $result = mysql_fetch_assoc(mysql_query("select unix_timestamp(NOW()) as timetest"));
    print_r($result);
Result:

Code: Select all

1084317560
Array
(
    &#1111;timetest] =&gt; 2004-05-12 01:20:04
)
Array
(
    &#1111;timetest] =&gt; 1084317604
)
As I prefer using timestamp's, and later use date() ( or whatever ) PHP function to format it, I need to use the MySQL's unix_timestamp() function.
More here: http://dev.mysql.com/doc/mysql/en/Date_ ... tions.html
Post Reply