help getting today's date into form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

help getting today's date into form

Post by jmilane »

Hi,

I have a form.

I need to return today's date in a hidden field.

I need the time stripped off of it - mysql format.

This is the field now:

<input value="0" type="hidden" id="qf_5d8d6e" name="custom_17" />

I need input value to be today's date, minus all the time stuff, and in a format mysql can take without choking.

I'd appreciate any help. I have tried a bunch of stuff, but none of it works.

J
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help getting today's date into form

Post by Christopher »

Code: Select all

<input value="0" type="hidden" id="qf_5d8d6e" name="custom_17" value="<?php echo date('Y-m-d'); ?>" />
Check the date() function's manual page for different date formats.
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can't you use the NOW() function in MySQL instead of passing the date to the DB?

Code: Select all

INSERT INTO `table` (`id`, `date`, `data`) VALUES (23, NOW(), 'Some string to enter');
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

my favorite way is to do this:

make a string, lets call it todayx

Code: Select all

$todayx = date("F j, Y @ g:i a");
now, lets echo it out:

Code: Select all

echo $todayx;
you can refference it the same way... $todayx

hope this helps.

-Matt
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

Everah wrote:Can't you use the NOW() function in MySQL instead of passing the date to the DB?

Code: Select all

INSERT INTO `table` (`id`, `date`, `data`) VALUES (23, NOW(), 'Some string to enter');
I could, but Id rather just strip the time off of it here. I need to break it up into year/month/day and handle each piece as a seperate field.

Thanks, all.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you read through the MySQL manual on date and time functions? They have something for everything you want to do. And doing it DB side is faster for your script than processing it code side.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

Everah wrote:Have you read through the MySQL manual on date and time functions? They have something for everything you want to do. And doing it DB side is faster for your script than processing it code side.
I have looked. This isnt straightforward. Thanks, though. I will look again. There is too much to learn... my head it going to pop.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

When NOW() is applied to a DATE field, only the date is kept. I would advise that you keep the date as a single field and allow your database to handle it as a real date.
Post Reply