This is a simple but effective tip that I picked up this week.
In the past, if I wanted to insert the current date into a table in a MySQL database from within a PHP script, I would always do something like this:
<?php
$current_date = date("Y-m-d H:i:s");
...
$query = "INSERT INTO guestbook SET date = '$current_date'";
$sql = mysql_query($query) or die(mysql_error());
?>
Then it was pointed out to me, that this is in fact an arse-backwards way of doing things, as the variable $current_date
will enter MySQL as a string, which MySQL will have to parse back into a DATETIME type.
A much better / faster / cleaner way to do things is using CURRENT_TIMESTAMP, which is a built-in MySQL function.
<?php
$query = "INSERT INTO guestbook SET date = CURRENT_TIMESTAMP";
$sql = mysql_query($query) or die(mysql_error());
?>
You can take this even further by constructing the date field in your database table thus:
date TIMESTAMP NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
Which means that it is automatically populated with the current date, the minute a record is created or updated.
how to insert date in each column of a table for every month?
Great one, Can we use the default current timestamp option available in php myadmin panel?
I would think so.
Why don’t you try it out and let us know if it works?
how do I apply the EST timezone to something like this:
Don’t do this. You will open yourself to SQL injection attacks.
You can alter the timezone to EST like so:
hi….i have a doubt in how to store the given date into the database instead of giving the current date…???
Thanks in advance…:-)
Hi,
Something like this: