Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp to get current time/date

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

like image 299
user2192221 Avatar asked Mar 20 '13 19:03

user2192221


People also ask

What is current_timestamp in SQL Server?

This function returns the current database system timestamp as a datetimevalue, without the database time zone offset. CURRENT_TIMESTAMPderives this value from the operating system of the computer on which the instance of SQL Server runs. Note

How do I get current date and time in SQL?

CURRENT_TIMESTAMP. The CURRENT_TIMESTAMP function is a SQL-standard function supported by almost all database systems such as DB2, Firebird, Microsoft SQL Server, MySQL, Oracle, PostgreSQL, and SQLite. The following statement returns the current date and time of the database server: SELECT CURRENT_TIMESTAMP AS 'Current date and time' ;

How to get current date and time from command prompt?

This post explains how to get current date and time from command prompt or in a batch file. To print today’s date on the command prompt, we can run date /t. Just running date without any arguments prints the current date and then prompts to enter a new date if the user wants to reset it.

How do I get the current time in Linux?

Get time from command prompt. Similar to date command, we have the command time which lets us find the current system time. Some examples below. c:>time /t 11:17 PM c:>time The current time is: 23:17:18.57 Enter the new time: c:>. As you can see, the command prints the time in different formats. It prints in 12 hour format when /t is added and ...


Video Answer


3 Answers

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);

?>
like image 189
Alex Avatar answered Oct 16 '22 21:10

Alex


The query will be:

 INSERT INTO mytable(ArrivalTime) VALUES(UNIX_TIMESTAMP())
like image 27
artahian Avatar answered Oct 16 '22 21:10

artahian


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())

like image 1
Aris Avatar answered Oct 16 '22 23:10

Aris