Storing date and time in mysql table

In this quick guide, I’ll be showing you how you can store date and time values on mysql table using php.

First thing that you need to do is to declare your database information.

<?php
 
$con=mysql_connect("localhost","root","1234"); //host, user, password
mysql_select_db("onstor",$con); //database and the connection information
 
?>

Then set your time zone. You can find a list of time zones that is recognized by php in this link: http://php.net/manual/en/timezones.php

I am living in the Philippines so my time zone would be declared this way:

date_default_timezone_set('Asia/Manila');

The most important part is the value of date that you’re going to store in the database.

$date=date('Y-m-d h:i:s');

That’s the date format that is recognized by mysql. You can modify it but it might store something like this, if you don’t get it right:

0000-00-00 00:00:00

And finally, the query that will store the value into the database:

mysql_query("INSERT INTO reports(DATE) VALUES('$date')");

That’s a quick guide on storing date and time values on mysql database. Hope you learned somethingSmile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s