2

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.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
user3627135
  • 61
  • 2
  • 10

5 Answers5

2

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.
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
1

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

asprin
  • 9,579
  • 12
  • 66
  • 119
0

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 ;

?>
user3209031
  • 837
  • 1
  • 14
  • 38
0

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?

Community
  • 1
  • 1
cardeol
  • 2,218
  • 17
  • 25
  • I have now set the correct time zone, next is how do I store it into to my database? – user3627135 May 12 '14 at 10:06
  • You should create a field in your mysql table using type as TIMESTAMP. Then just insert your php date using the code attached to this answer. – cardeol May 12 '14 at 10:08
0

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");
Gregor
  • 592
  • 7
  • 20