By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,617 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,617 IT Pros & Developers. It's quick & easy.

Put today's date into a MySQL "date" formated field

P: n/a
My MySQL table has a field that is set as type "date." I need to get
today's date, and insert it into that field. The default for that MySQL
field is 2006-00-00.

I know about the date() function. I have tried date("Y-m-d") and
date("U"), neither worked. The date field was just filed with all
zeros.

Can anybody point me in the right direction?

Thanx.

Aug 3 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
walterbyrd <wa********@iname.comwrote:
My MySQL table has a field that is set as type "date." I need to get
today's date, and insert it into that field. The default for that MySQL
field is 2006-00-00.

I know about the date() function. I have tried date("Y-m-d") and
date("U"), neither worked. The date field was just filed with all
zeros.

Can anybody point me in the right direction?

Thanx.
now()

Peter

--
http://www.boosten.org

Mail: peter at boosten dot org
Aug 3 '06 #2

P: n/a

Peter Boosten wrote:
>
now()
According to php.net: "Sorry, but the function now is not in the
online manual. "

Here is the actual code:

This *will* work:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], '2006-08-03')")
This will *not* work:

$today = trim(date("Y-m-d"));
.. . . .
mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], $today)")

Note: the only thing in the second example that will not is the date.
It always comes out as: "0000-00-00". I have tried the date function
with and without the trim().

I have also tried the time() function, that didn't work either.

Aug 3 '06 #3

P: n/a
"walterbyrd" <wa********@iname.comwrote:
Peter Boosten wrote:
>now()

According to php.net: "Sorry, but the function now is not in the
online manual. "

Here is the actual code:

This *will* work:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], '2006-08-03')")
now() is a MySQL function.

insert into inventory (date_modified) values (now());

miguel
--
Photos from 40 countries on 5 continents: http://travel.u.nu
Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
Airports of the world: http://airport.u.nu
Aug 3 '06 #4

P: n/a
walterbyrd wrote:
Peter Boosten wrote:
>now()

According to php.net: "Sorry, but the function now is not in the
online manual. "

Here is the actual code:

This *will* work:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], '2006-08-03')")
This will *not* work:

$today = trim(date("Y-m-d"));
. . . .
mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], $today)")

Note: the only thing in the second example that will not is the date.
It always comes out as: "0000-00-00". I have tried the date function
with and without the trim().

I have also tried the time() function, that didn't work either.
now() is a SQL function.

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], now())")

Or if you have access to your table you can change the default data to
CURRENT_DATE() for date_modified row. This way you do not have to add
any date into that field. Any time you do an insert\update the current
date\time will be updated automatically. Hence:

mysql_query("INSERT INTO inventory (item_num, location, quantity)
VALUES($row[0], 'FLR', $row[1])")

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 3 '06 #5

P: n/a
IchBin wrote:
walterbyrd wrote:
>Peter Boosten wrote:
>>now()

According to php.net: "Sorry, but the function now is not in the
online manual. "

Here is the actual code:

This *will* work:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], '2006-08-03')")
This will *not* work:

$today = trim(date("Y-m-d"));
. . . .
mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], $today)")

Note: the only thing in the second example that will not is the date.
It always comes out as: "0000-00-00". I have tried the date function
with and without the trim().

I have also tried the time() function, that didn't work either.
now() is a SQL function.

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], now())")

Or if you have access to your table you can change the default data to
CURRENT_DATE() for date_modified row. This way you do not have to add
any date into that field. Any time you do an insert\update the current
date\time will be updated automatically. Hence:

mysql_query("INSERT INTO inventory (item_num, location, quantity)
VALUES($row[0], 'FLR', $row[1])")
Sorry the DB format of the date_modified row in the Table definition
would look like this:

`date_modified` timestamp NOT NULL default '0000-00-00 00:00:00' on
update CURRENT_TIMESTAMP,
--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 3 '06 #6

P: n/a
walterbyrd wrote:
>
Peter Boosten wrote:
>>
now()

According to php.net: "Sorry, but the function now is not in the
online manual. "

Here is the actual code:

This *will* work:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], '2006-08-03')")
This will *not* work:

$today = trim(date("Y-m-d"));
. . . .
mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], $today)")

Note: the only thing in the second example that will not is the date.
It always comes out as: "0000-00-00". I have tried the date function
with and without the trim().

I have also tried the time() function, that didn't work either.
You need quotes around the date:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], '$today')")

or realise that now() is a SQL function and do:

mysql_query("INSERT INTO inventory (item_num, location, quantity,
date_modified) VALUES($row[0], 'FLR', $row[1], now())")

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Aug 3 '06 #7

P: n/a

IchBin wrote:
now() is a SQL function.
Tried this:

$day = now();

got this:

Fatal error: Call to undefined function: now()

I dunno. It doesn't seem to work for me. I'll try the quotes next.

Aug 4 '06 #8

P: n/a

Chris Hope wrote:
>
You need quotes around the date:
Thanx. That did it.
By the way, when putting together an sql statement, when do I use
single quotes, and when do I use \"$var \" ?? Or does it matter?

>
or realise that now() is a SQL function and do:
When I try t use now(), I get an undefined function error. I am using
PHP 4.4, under windows, if that matters.

Aug 4 '06 #9

P: n/a
walterbyrd wrote:
Chris Hope wrote:
>>
You need quotes around the date:

Thanx. That did it.
By the way, when putting together an sql statement, when do I use
single quotes, and when do I use \"$var \" ?? Or does it matter?
With mysql you can use single quotes and double quotes interchangeably.
Other databases are not so tolerant.
>or realise that now() is a SQL function and do:

When I try t use now(), I get an undefined function error. I am using
PHP 4.4, under windows, if that matters.
It's *not* a PHP function. It's a SQL function. So you put it in the SQL
query eg "INSERT INTO some_table (datefield) VALUES (now())"

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Aug 4 '06 #10

P: n/a
walterbyrd wrote:
>
IchBin wrote:
>
now() is a SQL function.

Tried this:

$day = now();

got this:

Fatal error: Call to undefined function: now()

I dunno. It doesn't seem to work for me. I'll try the quotes next.
That's because now() is *not* a PHP function, it's a SQL function. I
think this fact has been posted five times now, including in the post
you just replied to.

So you use it like this:

INSERT INTO some_tablename (datefield_name) VALUES (now())

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Aug 4 '06 #11

P: n/a

IchBin wrote:
`date_modified` timestamp NOT NULL default '0000-00-00 00:00:00' on
update CURRENT_TIMESTAMP,
Note: this requires mysql 4.1

Additional note (not at you IchBin):
NOW() is a SQL function.

SQL function. _~*Not*~_a PHP function.

Aug 4 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.