473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
11 21049
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

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
"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
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
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
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

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

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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ralph Freshour | last post by:
I have a Date type in my MySQL table - I'm trying to do a query on all rows within the last 30 days based on that Date field - I'm having trouble figuring out how to form the query??? $php_SQL =...
3
by: Noyb | last post by:
How do I convert a string into a value I can upload to mysql date field. Specifically, I have a user select a month, day and year from drop-downs, then I want them submitted to one date field....
2
by: Ricky | last post by:
I have a table with a field type date and null is set to yes. How can I insert a blank value without it being save as 0000-00-00. thanks Ricky
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
3
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004,...
3
by: Fran Zablocki | last post by:
I have a process that exports an Access table to a comma-delimited text file. One of the fields that is exported shows the date it was exported, using the Date() function. In the Access table, the...
2
by: hph | last post by:
Please be gentle; I am a relative newbie for php/MySQL, and most of what I am doing is based on being self-taught using someone else's well-written code, and my own googling the answers to my...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.