Time Stamp

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
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Time Stamp

Post 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
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post 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()]
Last edited by will on Thu Jul 18, 2002 12:49 am, edited 2 times in total.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post 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]);
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post 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).
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Post 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?
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post 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,
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Last edited by twigletmac on Sun Jul 21, 2002 5:54 am, edited 1 time in total.
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post 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.
Post Reply