Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: cddude on January 31, 2012, 06:05:11 PM

Title: time function
Post by: cddude on January 31, 2012, 06:05:11 PM
Hello.

I need to get the current forum time to be inserted into the database in order for me to complete a mod I have done.
I know what file and what function to call, I just don't know how. Would appreciate it ALOT if someone could write me the few lines of code :-\.

So I basically need to fill this gap with the variable which would pass the current forum time:
$sql="INSERT INTO column_name (time) VALUES (? ? ? ? ? ?) FROM table_name";

cheers  :).
Title: Re: time function
Post by: cddude on February 01, 2012, 01:17:55 PM
anyone?  :(
Title: Re: time function
Post by: cddude on February 02, 2012, 08:25:59 AM
bump
Title: Re: time function
Post by: Illori on February 02, 2012, 08:27:28 AM
cddude please do not bump within 24 hours we are all volunteers and reply if and when we know the answer

also if you provide some more details as to what you want to do maybe someone can understand and come up with a reply.
Title: Re: time function
Post by: cddude on February 02, 2012, 09:59:17 AM
I need to get the current forum time, I know I need to get it from the forum_time function in subs.php. I just don't know at all how to call it from an external file.
That one line of code is what I would like to know please.

Thanks.
Title: Re: time function
Post by: Joker™ on February 02, 2012, 10:05:57 AM
Any issue in using time() function?
Title: Re: time function
Post by: cddude on February 02, 2012, 10:07:29 AM
No, I don't know how to use it at all  :P

Could you help me with the code? How would I call the function to give me the current forum time???

cheers mate.
Title: Re: time function
Post by: Joker™ on February 02, 2012, 10:15:02 AM
$time = time();

Code (Using very basic sql query) Select

$sql="INSERT INTO table_name (column_name_time) VALUES ('$time')";



Also if you are planning to release your mod on SMF, don't forget to read the Customization coding guidelines (http://wiki.simplemachines.org/smf/Customization_coding_guidelines).
Title: Re: time function
Post by: cddude on February 02, 2012, 10:27:21 AM
Thanks Joker. Still have a few questions though.

You give the variable $time the return value of time() function, but in subs.php, the function is called forum_time(). Is that what you meant, or are we thinking of different functions?

Also, not sure how to include the subs.php file.
Like this?
INCLUDE 'http://www.mysite.com/sources/subs.php' ;
And should I put include or require?

I think that's all I need.

Thanks!  :)
Title: Re: time function
Post by: oOo--STAR--oOo on February 02, 2012, 11:26:21 AM
Quote from: Joker™ on February 02, 2012, 10:15:02 AM
$time = time();

Code (Using very basic sql query) Select

$sql="INSERT INTO table_name (column_name_time) VALUES ('$time')";



Also if you are planning to release your mod on SMF, don't forget to read the Customization coding guidelines (http://wiki.simplemachines.org/smf/Customization_coding_guidelines).


Use this what he said then to display it on your template would look like this

echo timeformat($row['time']);

Hope this helps if you was looking for this as an answer?

Title: Re: time function
Post by: cddude on February 02, 2012, 11:37:32 AM
template? I'm not doing a template, a custom mod.
I want the current forum time to be saved in the database when the user makes a specific action.

Exactly like the forum time is saved in smf_messages under poster_time when a user submits a post or topic.

I need the whole thing if possible. I don't know how to include or import the subs.php file into my mod-file, and I don't know how to call the forum_time function.

Or for example, say I want to make a mod where you click a button, and the current forum time and date gets stored in the database in its own independent table/column.
I know how to get and display data from the database, so I don't need to know that. I just need to know how to GET the current forum time when requested by the user.

I guess I should have explained it better...  ;D
Title: Re: time function
Post by: oOo--STAR--oOo on February 02, 2012, 11:43:25 AM
Quote from: cddude on February 02, 2012, 11:37:32 AM
template? I'm not doing a template, a custom mod.
I want the current forum time to be saved in the database when the user makes a specific action.

Exactly like the forum time is saved in smf_messages under poster_time when a user submits a post or topic.

I need the whole thing if possible. I don't know how to include or import the subs.php file into my mod-file, and I don't know how to call the forum_time function and save it to a variable.

I guess I should have explained it more...  ;D

Kk well lets make this more simple for you

Insert the time using the quote from jokers post then to display it you would do a query to SQL and grab the row for the time and to display it in the correct format you would echo the result from the query like I stated above.. Template meaning the section you display..

As far as I know you are supposed todo all your queries in a different file than your template.. Template is the output.. The information you want to display.. Like sources is where all the database queries are then in your themes folder you have the templates which echo the results in the fashion you want them.. IE keeping them separate..

Hope that makes sense to you.
Title: Re: time function
Post by: oOo--STAR--oOo on February 02, 2012, 11:54:23 AM
Maybe that is a big jarbled for you..

So do what joker said then to echo the results you would query the row like this

$query = $smcFunc['db_query']('', '
SELECT time
                FROM {db_prefix}Table_Name',
        array()
);

$row = $smcFunc['db_fetch_assoc']($query);

echo timeformat($row['time']);



Title: Re: time function
Post by: cddude on February 02, 2012, 11:58:06 AM
Ok, so this code would call the time function and save it to a local variable. So far I'm with you:
$time = time();
I also understand how to output the forum time later:
echo timeformat($row['time']);

What I don't understand is how I am going to "tell" the server that the time() function is not found in the same file, meaning I would have to point it to the right directory and folder. Just like you do #include in C/C++ or import in Java - correct?

Another thing I'm not sure of is the name of the function itself. Is the function really time() and not forum_time()?
Ain't this the file and function I need? ---> http://support.simplemachines.org/function_db/index.php?action=view_function;id=218
Title: Re: time function
Post by: Joker™ on February 02, 2012, 12:07:41 PM
time() is an inbuilt PHP function. To know more about it read the below link

http://php.net/manual/en/function.time.php
Title: Re: time function
Post by: oOo--STAR--oOo on February 02, 2012, 12:11:04 PM
Quote from: cddude on February 02, 2012, 11:58:06 AM
Ok, so this code would call the time function and save it to a local variable. So far I'm with you:
$time = time();
I also understand how to output the forum time later:
echo timeformat($row['time']);

What I don't understand is how I am going to "tell" the server that the time() function is not found in the same file, meaning I would have to point it to the right directory and folder. Just like you do #include in C/C++ or import in Java - correct?

Another thing I'm not sure of is the name of the function itself. Is the function really time() and not forum_time()?
Ain't this the file and function I need? ---> http://support.simplemachines.org/function_db/index.php?action=view_function;id=218

Its just part of PHP time(); will input the time into a int that can later be converted when you query the database to get the time.
In your database will look like this.
1328202216

There are many ways of displying the time you can refer to the php manual for displaying the time format in anyway you wish but the main thing is you would like to display the time for the current time format of the user in SMF so the above would be your best option.

Alternatively you could echo the result like this

$time = date('d-M H:i:s', $row['time']);
echo $time;


which would result in 02-Feb 17:04:20

Check the manual for other combinations / possibilities..

Will give you an idea of the outputs.



Title: Re: time function
Post by: Matthew K. on February 02, 2012, 12:29:41 PM
First of all...if you are planning on releasing an SMF Modification, great! But you need to make sure it follows the Customization guidelines.

SMF is built using an MVC (Model-View-Controller) meaning that Source/Logic HAS to be separated from the View/Template. It's become slightly more relaxed lately, although normally you'd even want to put timeformat(); in a Source file and feed the variable to the template.

$query = $smcFunc['db_query']('', '
SELECT time
                FROM {db_prefix}Table_Name',
        array()
);

$row = $smcFunc['db_fetch_assoc']($query);

echo timeformat($row['time']);
This code isn't quite correct...
Title: Re: time function
Post by: cddude on February 02, 2012, 01:42:47 PM
Quote from: Joker™ on February 02, 2012, 12:07:41 PM
time() is an inbuilt PHP function. To know more about it read the below link

http://php.net/manual/en/function.time.php

Oh, shoot. I had no idea about this! My bad.
Thanks for the heads up! I should get it to work then. I will mark this topic solved if I get your code to work Joker. Thanks for everyone else for input. Appreciate it. :)

Cheers.
Title: Re: time function
Post by: cddude on February 02, 2012, 01:52:59 PM
Yea code worked. All I needed was the $time = time(); haha, i wish I knew that.  :-[ ::)
Thanks again all. Topic marked as solved.
Title: Re: time function
Post by: Joker™ on February 02, 2012, 01:54:22 PM
No problem mate ;). Nice to see that codes are working out for you.
Title: Re: time function
Post by: MrPhil on February 02, 2012, 02:42:13 PM
Just a side note... be careful if mixing PHP's time() and MySQL's now(). They may not be talking the same time zone. time() is supposed to be GMT (UTC), while now() I've seen configured to be the server's time (usually not GMT).