getting auto the date and time
Moderator: General Moderators
getting auto the date and time
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
Is this posible? If so, wher and what information do I need to make this script.
Thx Skywalker
assuming a mysql-database you might use thiswhere dtWhen is e.g. of type DATETIME
http://www.mysql.com/doc/en/Date_and_ti ... ml#IDX1287
Code: Select all
$query = 'INSERT INTO tablename (dtWhen, ...) VALUES(Now(), ...)';
$r = mysql_query($query, ....http://www.mysql.com/doc/en/Date_and_ti ... ml#IDX1287
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;
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Have a look at this part of the manual for more info:
http://www.php.net/manual/en/ref.datetime.php
Mac
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
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
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
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));
?>- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
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");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; ?>
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
The manual really is your friend
Mac
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-14Mac
which leads to the queryto retrieve the current date/time and 3 months in the future. You may use this in an INSERT-statement as well
Code: Select all
SELECT Now(), Now() + INTERVAL 3 MonthI 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
I am going to try al the abouve things when I'm having enyproblems, I'll will be back.
Greathings Skywalker