Inserting todays date into the table.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

fspilot
New php-forum User
New php-forum User
Posts: 26
Joined: Fri Jun 27, 2003 11:51 am
Location: Bath UK
Contact:

Inserting todays date into the table.

Postby fspilot » Sun Aug 31, 2003 1:20 pm

Hi Everyone,

Im currently working on a registration form and want to have the date registered inserted into the table when someone registers.

How would i do this? I want to have the date registered only and no time etc.

Regards

toweter
New php-forum User
New php-forum User
Posts: 26
Joined: Sat Aug 30, 2003 9:04 am

Postby toweter » Sun Aug 31, 2003 1:32 pm

Code: Select all

$date = getdate();
$date = ($date[mon]."/".$date[mday]."/".$date[year]);

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sun Aug 31, 2003 4:01 pm

toweter wrote:

Code: Select all

$date = getdate();
$date = ($date[mon]."/".$date[mday]."/".$date[year]);


If you want to do it in PHP, the above is overly complicating things. Just use date(), which allows you to format it however you like (for a list of all of the formatting codes, see the documentation).

Code: Select all

<?
   $date = date('Y-m-d');
   echo $date;
   /* output:
         2003-08-31
   */
?>


To do this in MySQL, just use CURDATE():

Code: Select all

INSERT INTO table1 (mydate) VALUES (CURDATE());
# Inserts '2003-08-31' into the mydate column of table1


By the way, toweter, your usage of associateive arrays is wrong. If you're accessing an array element by a string index (e.g. "mday"), you have to pass the index as (surprise!) a string. In other words, you have to enclose it in quotation marks:

Code: Select all

<?
   $a = array('somekey' => 42);

   // THIS IS WRONG:
   echo $a[somekey];

   // This is correct:
   echo $a['somekey'];

   // This is also correct
   $key = 'somekey';
   echo $a[$key];

   /* note that when passing a string variable, you don't
      have to put quotation marks around it
   */
?>


The way you've done it, toweter, works for reasons of backward compatability (I so wish they'd get rid of it), but it's deprecated functionality and should not be used as a) it's syntactically wrong, which should be reason enough, and b) it makes PHP do extra work. You can read more about this in the documentation on Arrays. Scroll down to the section that starts like this:

Array do's and don'ts
Why is $foo[bar] wrong?

You should always use quotes around a string literal array index. ...

toweter
New php-forum User
New php-forum User
Posts: 26
Joined: Sat Aug 30, 2003 9:04 am

Postby toweter » Mon Sep 01, 2003 7:19 am

In other words, you have to enclose it in quotation marks


Yes, I know...
I wasn't sure if I'm right with the date-function, so I have copyed it from that Tutorial...


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron