user3627135
user3627135

Reputation: 61

Storing Time in MySQL using PHP

I am having a hard time storing a record of Time in MySQL using PHP and I hope someone would help me.

What I really need is to get my computer's time and store it into my database. I tried using this

$time_in = date ("h:i:s:a:");

and then store it into my database, but the problem is, it's showing an incorrect time.(+6:00, I think)

So, how do I get my own computer's current time and store it as it is in my database? Thank you in advance.

Upvotes: 2

Views: 6872

Answers (5)

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

Here is the Code that you need :

<?php
date_default_timezone_set('Asia/Calcutta'); 
$time = date('H:i:s');
$date = date('d-m-Y');
echo "Date is ".$date."Time is ".$time;
echo $date1;
?>

To store it in Database :

<?php
$con=mysqli_connect("localhost","username","password","my_db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,"INSERT INTO date_time (Date, Time)
VALUES ('$date', '$time')");


mysqli_close($con);
?>

Note :

date_default_timezone_set('Asia/Calcutta'); 
  1. Change the Location according to yours.

Here is the List of Supported Timezones Officially php.net

  1. Create a table called as date_time before running this code.

Upvotes: 2

cardeol
cardeol

Reputation: 2238

You should use date_default_timezone_set for your area and use a timestamp field in your mysql table.

more info:

http://php.net/manual/en/function.date-default-timezone-set.php

<?php

date_default_timezone_set('Europe/Dublin'); 
$myDate = date("Y-m-d H:i:s"); // mysql timestamp: (YYYY-MM-DD HH:MM:SS)
mysql_query("insert into `mytable` set `mytime` = '$myDate'");

?>

or you can set the database timezone with an offset and use mysql date/time functions.

mysql:

SET time_zone='offset';

more info: How do I set the time zone of MySQL?

Upvotes: 0

Gregor
Gregor

Reputation: 622

Your database knows your computer's time too. Just use the mysql function NOW() to add current time in your database. Something like

INSERT INTO mytable (mydata, created) values ("some data", NOW());

This works perfectly if the database is on the same computer than the executing script.

If you need to send the date of your computer to a database on a remote computer, then this is the correct way to get your current time in mysql date format

$time = date("Y-m-d H:i:s");

Upvotes: 0

user3209031
user3209031

Reputation: 835

You Can try this :

<?php

        $time_nowb=mktime(date('h')+11,date('i')+30,date('s')-15);  
        $added_date = date('Y-m-d h:i:s',$time_nowb);
        echo $added_date ;

?>

Upvotes: 0

asprin
asprin

Reputation: 9823

You need to use the timezone settings and set it according to your region before inserting. For example:

date_default_timezone_set('America/Kentucky/Louisville');
$time_in = date("h:i:s");
// perform insertion

For list of supported timezones, refer PHP Timezones

Upvotes: 1

Related Questions