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.

4
  • It's very unlikely that you get this small time difference. The main reason I can think of is that there is a 6 minute time difference between your computer and the server that runs PHP. Commented May 12, 2014 at 9:49
  • Have you tried using raw SQL to achieve this? Commented May 12, 2014 at 9:51
  • Sorry for the incorrect format @GolezTrol, that is supposed to be +6 hours and I am using my computer as the server too. Commented May 12, 2014 at 9:57
  • @Jonast92 I am sorry but I have no idea how to try this using raw SQL. Commented May 12, 2014 at 9:57

5 Answers 5

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.
Sign up to request clarification or add additional context in comments.

9 Comments

Oh I got it. I just changed the value to ('Asia/Taipei'). Thank you!
@user3727135: Kindly change the time zone to your location, date_default_timezone_set('##to your location');
I have now set the correct time zone, next is how do I store it into to my database?
mysqli_query($con,"INSERT INTO Table (date) VALUES ($date1)"); You should create a table and a row called as date in it and you should give the proper MySqli Connections
Mysqli Insert Here is the link to insert data using mysqli
|
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

Comments

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 ;

?>

Comments

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

1 Comment

I have tried using NOW() but I need the Date and Time records to be saved in the database separately.
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?

2 Comments

I have now set the correct time zone, next is how do I store it into to my database?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.