getting auto the date and time

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
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

getting auto the date and time

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

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

Post 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
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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));
?>
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

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

Post 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
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

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

Post 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 :wink:

Mac
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
Skywalker
Forum Contributor
Posts: 117
Joined: Thu Aug 29, 2002 3:33 am
Location: The Netherlands

Post 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. :roll:

Greathings Skywalker
Post Reply