Page 1 of 1
getting auto the date and time
Posted: Mon Jan 20, 2003 7:08 am
by Skywalker
I want to get with a php script the date and time of the machine on submisions of an form. The data and time must then be saved in the database.
Is this posible? If so, wher and what information do I need to make this script.
Thx Skywalker
Posted: Mon Jan 20, 2003 7:20 am
by volka
assuming a mysql-database you might use this
Code: Select all
$query = 'INSERT INTO tablename (dtWhen, ...) VALUES(Now(), ...)';
$r = mysql_query($query, ....
where dtWhen is e.g. of type DATETIME
http://www.mysql.com/doc/en/Date_and_ti ... ml#IDX1287
Posted: Mon Jan 20, 2003 9:21 am
by Skywalker
Is this a correct way to put a date with time in to a variable?
Code: Select all
<?php
$Timestamp = date("D","d","m","Y","i","s");
echo $Timestamp;
?>
Posted: Mon Jan 20, 2003 9:29 am
by twigletmac
If you're trying to get a timestamp then using the
date() function won't work (date() is for formatting UNIX timestamps into string formats such as dd/mm/yyyy). To generate a UNIX timestamp you would generally use the
mktime() function.
Have a look at this part of the manual for more info:
http://www.php.net/manual/en/ref.datetime.php
Mac
Posted: Mon Jan 20, 2003 9:44 am
by Skywalker
So what you are trying to say is that the date function, just conferting the mktime function to the way you want?
If that isn't what you ment I don't understand it. The machine wher it's running on is an Linux machine. Maybe that is a bit more clear.
I want the date year etc and the time. insert in to variable and after that, to insert the variable into the database as tekst.
I do not understand what I have to use, do I have to use the date function and the mktime function or only the mktime function?
Greathings Skywalker
Posted: Mon Jan 20, 2003 10:40 am
by volka
why do you need the current date/time in a variable when all you want is to store it in a database?
When selecting a DATETIME-field from mysql it ships (like all types) as string anyway. Try
Code: Select all
<?php
// <- code to connect to mysql here ->
$result = mysql_query('SELECT Now()') ;
echo array_shift(mysql_fetch_row($result));
?>
Posted: Mon Jan 20, 2003 12:42 pm
by puckeye
Skywalker wrote:Is this a correct way to put a date with time in to a variable?
Code: Select all
<?php
$Timestamp = date("D","d","m","Y","i","s");
echo $Timestamp;
?>
If you want a straight date as "Mon 20-01 13:40:00" the above code won't work. This would work
date("D d-m H:i:s");
You should really take a look at the PHP manual it's very handy and easy to find what you need:
http://www.php.net/manual/en/function.date.php
Posted: Tue Jan 21, 2003 2:29 am
by twigletmac
Definitely go with what volka told you, let MySQL do the work there's no point creating a variable in PHP just to put it into a database.
Also to reiterate what puckeye said - the manual really does have a lot of useful information in it:
http://www.mysql.com/doc/en/Date_and_ti ... ml#IDX1287
Mac
Posted: Tue Jan 21, 2003 3:02 am
by Skywalker
But what if I want to calculate the next date based on the date and time now?
What if the date now is 21-01-03 09:53:21 and i want to calculate the time and date 3 months ahead so I will get the date 21-03-03 09:53:21
I want that one also in to the database.
Posted: Tue Jan 21, 2003 3:08 am
by twigletmac
Have you read through MySQL's date and time functions?
http://www.mysql.com/doc/en/Date_and_ti ... tions.html
If you go through the page you should come across the
DATE_ADD() function which has an example similar to this attached:
Code: Select all
mysql> SELECT DATE_ADD('1998-01-14', INTERVAL 1 MONTH);
-> 1998-02-14
The manual really is your friend
Mac
Posted: Tue Jan 21, 2003 3:12 am
by volka
which leads to the query
Code: Select all
SELECT Now(), Now() + INTERVAL 3 Month
to retrieve the current date/time and 3 months in the future. You may use this in an INSERT-statement as well
Posted: Tue Jan 21, 2003 3:18 am
by Skywalker
I always try to understand the documentation of the functions, but my english is to bad to understand precise what they mean un the documentation, that means often I ready It but I still don't understand it.
I am going to try al the abouve things when I'm having enyproblems, I'll will be back.
Greathings Skywalker