Page 1 of 1
Time Stamp
Posted: Thu Jul 18, 2002 12:26 am
by ace2600
Is there any php code I can use to include the date a user submitted his form and add it to the db. Thanks for the help!
-Ace
Posted: Thu Jul 18, 2002 12:39 am
by will
there are a number of ways to do this. personally, i like to create a field in the database that is a bigint. the php function mktime() returns a unix timestamp, and this number can be inserted into the field you just created.
i prefer the field type of bigint versus datetime because the php date() function takes in a unix timestamp, and this makes it all much easier. on the flip side, however, mysql has severl built-in functions for dealing with time, so you may prefer to go that route. in such case set "date_field=now()" in your SQL query.
[edit: yeah, that should be time(), not mktime()]
Posted: Thu Jul 18, 2002 12:45 am
by ssand
I use time() at the begining of the form processing script and then just add the value to the db. Then depending on how you are redisplaying the information you can use the PHP date() to format it.
Code: Select all
/// Get the time
$time = time();
Then insert the $time value into the database.
When you eventually query the database for the value you can use the PHP date formats.
echo date ("m/d/Y", $qryїtime]);
Posted: Thu Jul 18, 2002 2:18 am
by haagen
As will said mysql has several built in functions. These are named:
CURRENT_DATE(), CURRENT_TIME() and CURRENT_TIMESTAMP().
I prefer using them because then your sql-queary is as mych static as possible (no need to check contents of variables).
Posted: Fri Jul 19, 2002 7:56 pm
by ace2600
Thanks for the help. I want to make some queries that deal with date calculations (in php). If I go with "field_name = CURRENT_DATE()" will I be able to add 15 days with mysqls variable for the date or must I use the Unix time stamp to do so?
Posted: Fri Jul 19, 2002 11:11 pm
by EricS
You will want to use the DATE_ADD function in MySQL to move forward a certain number of days. I haven't found a PHP function with this ability so I would make it a part of my SQL calls because coding it in PHP will be a little tricky with different day numbers in months and special dates like leap year.
Hope this helps,
Posted: Sat Jul 20, 2002 2:03 am
by ace2600
Well I tried php's time() and inserted the value into a bigint field. I got this as a result in the field when I submitted it: "1027148285". Is that good, and if so how do I read it?
Posted: Sat Jul 20, 2002 5:23 am
by twigletmac
That's called a timestamp and there's a quick explanation of it in the man page for
time(). As for what you can do with it now, have a look at PHP's
date and time functions and
MySQL's.
Mac
Posted: Sat Jul 20, 2002 11:26 pm
by will
EricS wrote:<snip> because coding it in PHP will be a little tricky with different day numbers in months and special dates like leap year.
Hope this helps,
no it wouldn't... that's the beauty of the unix timestamp... it's simply the number of seconds since the epoch (january 1, 1970 if i remember correctly).
to increment by 15 days, you would get your unix timestamp and add 1296000 (60 * 60 * 24 * 15). then when you convert it to whatever format you want using date(), the month, leap year, etc will be taken into account.
actually, i'm pretty sure that the mysql datetime field type uses the same unix timestamp, it's just does it all behind the scenes.