MySQL Timestamp - why all zeros? - php

I'm using PHPMyAdmin and I've got a MySQL table column called "timestamp." The type (surprise!) is TIMESTAMP, and in 'attributes' I've set it to ON UPDATE CURRENT_TIMESTAMP.
However, each new record gets a timestamp that looks like this:
0000-00-00 00:00:00
I have explicitly set the default value to none, but when I save and come back to look, it is set to all zeros as above.
The relevant PHP records page hits with this query:
$query = "INSERT INTO `pagehit` (user_id, pageurl)
VALUES ('" . $userid . "', '" . $pageurl . "')";
The whole thing is running under XAMPP.
What am I missing?

What am I missing?
You don't update :)
Use DEFAULT CURRENT_TIMESTAMP along with ON UPDATE CURRENT_TIMESTAMP

Try setting the default value to CURRENT_TIMESTAMP instead of putting that in the attributes.
MySQL Reference

If your timestamp column captures only the insertion time then use only
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Otherwise if it is for modification time then use like as follows
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

In my case works like this:
In phpMyAdmin:
ALTER TABLE `name_table` ADD `name_row` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
In PHP sintax for the row:
date('Y-m-d H:i:s')

Related

How to Manually Adjust Value of timestamp To Previous Year?

I am using timestamp to store the pubdate of uploads. I want to add a filter to sort records by the year they were uploaded so need to change a couple of records to previous years for testing purposes. When I try to manually adjust the date from within mysql by clicking the calendar and picking a date it flags errors.
The value of timestamp is set to CURRENT_TIMESTAMP.
The values of function in the timestamp field are :
NOW
CURDATE
FROM_DAYS
FROM_UNIXTIME
PERIOD_ADD
PERIOD_DIFF
TO_DAYS
I cannot find anything online relating to this. I don't need to perform calculations I need to manually adjust the date.
In case the problem is that you used an ON UPDATE CURRENT_TIMESTAMP on this column you can remove this setting by redefining your column:
ALTER TABLE yourTable MODIFY COLUMN pubdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
You might also check the docs:
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP,
it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP.
And I guess your table was created without defining any of those two resulting in the behavior that you cannot update this column as it is updated automatically.
What happens is:
You pick a row and overwrite the timestamp (like setting the date to 2013)
MySQL detects that the row was updated
as ON UPDATE CURRENT TIMESTAMP is implicitly specified MySQL updates the timestamp field with the current timestamp
Your changes to the timestamp will be overwritten / lost

Problems with timestamp on mysql (php)

I have made the following table on mysql to save the time that a client visit the page:
CREATE TABLE `visitas` (
`**idv**` int(11) AUTO_INCREMENT,
`**fecha**` timestamp **DEFAULT CURRENT_TIMESTAMP**,
`**ip**` varchar(15)
PRIMARY KEY (`**idv**`))
On php I run the next query:
mysql_query("INSERT INTO `visitas`(`ip`) VALUES ('$ip')");
The problem is that mysql save the time of the United States, and I want the time of Mexico.
I have tryed:
mysql_query("SET time_zone = '-06:00'");
but it doesnt take effect.
Please help me to make that mysql automatically save the time that i want.
To set timezones with mysql, use SET GLOBAL time_zone = '-06:00';
or just set it with php
mysql_query("insert into visitas(ip, timestamp) values ('$ip', '". date('Y-m-d H:i:s', time()) ."');
also, dont use mysql, look at mysqli or PDO

Insert current date to the database?

How do I insert the current date to my database? I have a column called date to put it on.
I want to insert it at the same time I insert this:
$sql="INSERT INTO `Lines` (Text, PID, Position)
VALUES
('$text','$pid','$position')";
Is there a way to automate it in PHPMyAdmin or it's the same to do it this way? Thanks
If the table definition has the timestamp column default set to CURRENT_TIMESTAMP, you actually don't have to do anything at all. Otherwise, NOW() and CURRENT_TIMESTAMP will work, as in:
INSERT INTO t1 (timestamp_column) VALUES (NOW());
There is a difference between CURRENT_TIMESTAMP and NOW() but it's probably too small to matter to you.
phpMyAdmin seems like it has CURRENT_TIMESTAMP as an option when creating a new column.
INSERT INTO `Lines` (`date`) VALUES (NOW());
Depending on your requirement, you can also do this
$date=date('d.m.y h:i:s');
And then insert $date. I mean if you only want to view the date & time. Otherwise i also recommend time().
Otherwise... if you're using an integer, as is common in PHP, just use time() and insert it's value the same way you inserted the other variables, or use MySQL's UNIX_TIMESTAMP()
The best way to store timestamps is as UNIX timestamp integers in GMT / UTC. This allows you to always know the exact time no matter where you, your server, or your users are located. A bonus is that you can allow your users to set their timezone and display times meaningful to them.
$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ('" . time() . "')";
or
$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ( UNIX_TIMESTAMP() )";
Be careful if you choose to use NOW() or CURRENT_TIMESTAMP as they are managed by the database server and it's settings. If the server your site is hosted on is in one time zone and you move to another host in another timezone all of your timestamps will be incorrect. Using Unix integers will add a little extra effort wherever you application deals with times but it gives you the most accuracy and the most flexibility.

Set current time field for MySQL

I am trying to set the value of a field via a hidden form field to the current date and time using either PHP or Javascript that would conform to MySQL's datetime field.
You can use PHP to get and format the current system date/time for use in MySQL like this:
$now = date('Y-m-d H-i-s');
You can directly set current date and time in your SQL insert query using NOW():
INSERT INTO table_name (current_time, column2, column3,...)
VALUES (NOW(), value2, value3,...)
where current_time is the field where you want to put current date and time.
Create the column using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP
Those together will make it so that any new rows inserted have the current time and are updated again when the column is updated.
Example:
CREATE TABLE test (last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
Edit: Nevermind, this will use a TIMESTAMP column, not DATETIME. Other answers will do what you want.
<?php echo time(); ?>
will output a nice simple integer number that you can pass directly into MySQL and convert into a native mysql datetime value with FROM_UNIXTIME(). It'll save you the trouble of formatting the data in a nice YYYY-MM-DD hh:mm:ss string.
$current = date_timestamp_set(date_create(), time());

SQL: Insert DateCreated value?

In my table I have a dateCreated column in the format of DATETIME.
How do I insert the current datetime when I add the record to the database?
Better use TIMESTAMP instead and use CURRENT_TIMESTAMP to get current timestamp. TIMESTAMP should behave exaclty (or very similar) to DATETIME so you shouldn't notice a difference.
Furthermore you may set CURRENT_TIMESTAMP as default for all new entries.
Use the CURDATETIME()

Categories