5

I need simple code using PHP to get the current date/time using TIMESTAMP and insert this into a database.

I have a field called "ArrivalTime" within a database as TIMESTAMP.

EDIT:

<?php $con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error()); mysql_select_db("a&e", $con) or die('Could not select database.'); 

// validate

$time = time(); $date = date('Y-m-d H:i:s',$time); $sql="INSERT INTO Patient(Forename, Surname, Gender, Date_Of_Birth, Address, Patient_History, Illness, Priority, Arrival_Time) VALUES('$patient_name', '$patient_lastname', '$gender', '$date', '$address', '$history', '$illness', '$priority', '$time')"; mysql_query($sql,$con) or die('Error: ' . mysql_error()); echo "1 record added"; // close connection mysql_close($con); ?> 

Many thanks

3 Answers 3

8

main.php

<?php require('connect.php'); $time = time(); $sql = "INSERT INTO yourtablename (ArrivalTime) Values ('$time')"; mysql_query($sql); ?> 

P.S: in the sql statement i'm sure you'll need to put other things in the other fields ,so you just replace the one above by this:

$sql = "INSERT INTO yourtablename (field1, field2, ArrivalTime) Values ('$value1','$value2','$time')"; 

connect.php

<?php $error = "Couldn't connect"; $connect = mysql_connect("localhost","username","password") or die($error); mysql_select_db("yourdatabase") or die($error); ?> 
Sign up to request clarification or add additional context in comments.

16 Comments

That's great, many thanks. Will I need to incorporate php into my form as a hidden string to echo the time into the database?
it depends what do you intend to do exactly? Are you using html to post values?
those are 2 separate files: main.php and connect.php, you place them in the same folder as your html file.
I need the exact arrival time/date of a patient. The form is to collect patient information and store it in the database. I have used POST functionality to post the patient values for example: Forename into the database. Do I need to do this for the time? (I don't want the time to display on the form, only the database)
the time will not display on the form, just remove the echo. I'll edit it
|
3

The query will be:

 INSERT INTO mytable(ArrivalTime) VALUES(UNIX_TIMESTAMP()) 

Comments

1

mysql has a function UNIX_TIMESTAMP() for getting a Unix timestamp as an unsigned integer.

Example:

mysql> SELECT UNIX_TIMESTAMP(); -> 1196440210

So you can use this sql query:

insert into tableName(ArrivalTime) values(UNIX_TIMESTAMP())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.