Timestamp to get current time/date

48,737

Solution 1

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

?>

Solution 2

The query will be:

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

Solution 3

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

Share:
48,737
user2192221
Author by

user2192221

Updated on January 12, 2020

Comments

  • user2192221
    user2192221 over 4 years

    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