How to input the current time into a mysql table from PHP - php

I'm using a mysql_query() in PHP to insert a value into a table in a database. One of the attributes of that table is "timestamp" which is supposed to be the current time that the query was made. How can I generate this? I tried using "time()" in PHP, but this is not inserting correctly in the query. When I insert what the time() function generates, it shows up as a default in my table, as: "0000-00-00 00:00:00". How can I generate the current time in order to input it into my table?

In your query, you need to set the value of that field to NOW() which is MySQL's function for getting a timestamp of the current time.
If you put your actual query in your question, I can help you with a more specific answer ;)

insert into table (datetime_field) values (now())
php function time() returns a unix timestamp that it's an integer value.

Go to php.net and view the codes for getting date and time and check whether you typed the codes correctly. If you did, then check if the format you are using in your code is similar to the one you are using in your table.

Related

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Php/Mysql date saved as '0000-00-00'

I have a Mysql database where i keep dates of the clients orders.
Date is generated by a php function date('Y-m-d'). Two records in my database have values '0000-00-00'. One was created by a client from Australia and another by a client from Italy. Other 5000 orders are fine.
My question is how is it possible to generate such date ? Could it be something with individual browser settings ?
Thanks for help
The only reason what i can find is either your client has entered improper date format or he has entered an empty string.
Remember the date format in Mysql is yyyy-mm-dd.
I noticed I was having the same exact issue, and it turned out it was simply that I didn't put single quotes around my $date variable when inserting it, thus it was inserting a null or blank value which appeared as 0000-00-00 in the database. Once I put '$date' instead of $date in my insert statement, the value appeared as expected.
It means MySQL was passed an invalid value including no value at all. Check your code to make sure that it always has a valid value for that date.
Check this post default date '0000-00-00 00:00:00' or null. It will answer your question. Probably, year, month and day variables at their client side code have not been initialized.
EDIT
If that's not the case, then it seems you have recently changed the Date-related column in the database from VARCHAR or TEXT to DATE.
MySql changes all NULL values in this case to '0000-00-00'. So, if some time back, date has not been a compulsory field on the UI and if it was passed as NULL, then the above thing has happened.
It could be the case that you have purely programmed or outdated mysql function. For instance: CURDATE() + 5 will insert the date correctly in the DB, however if the user does it on 2016-12-28 the DB will show 0000-00-00. It will work fine if it the current date is 2016-12-20. The absolutely correct function would be CURDATE() + INTERVAL 5 DAY.
You write at the beginning of Jan, so maybe u have similar issue.

Mysql 2 date type

Can I use 2 date type in 1 mysql table? because for some reason when I insert records for 2 different dates it doesnt work, only the first date is working the other one shows 00-00-00?
Thank you for your time and help
I will assume you are using a TIMESTAMP data type, and you want it to default to CURRENT_TIMESTAMP.
Only one TIMESTAMP column in a table can automatically be set on INSERT or UPDATE - and by default, MySQL goes with the first one.
If you would like to have more than one TIMESTAMP column get updated automatically on INSERT or UPDATE, you will need to turn to triggers.
The docs on the TIMESTAMP data type are a good read!
Edit: if this educated guess does not answer your rather vaguely-phrased question, you will probably want to update your question with an example of the queries you are running, and what exactly is happening/not happening afterward.

Question regarding MySQL timestamp comparisons

I'll explain my goal first: I want the user to query the database, and return rows only if those rows have been updated since their last query. No sense returning data they'd already have. So I created a column called 'lastupdated', a timestamp type which autoupdates every time any content in the row is updated. This works fine. Now, I want to form the query correctly. The user will have their previous query's timestamp saved, and via php will use it to compare their previous query's time with the time each row has been updated. If the row was updated after their last query, the row should be returned.
I made something like this,
SELECT * FROM users WHERE '2011-02-26 01:50:30' <= lastupdated
but its obviously much too simple. I checked the MySQL manual and found this page MySQL Time/Date Page. I'm sure the answer is here, but I've read through it any nothing really makes sense. I have a timestamp in the same format used by the MySQL timestamp type, but I don't know how I will compare them. Thank you very much for your help.
That query is exactly how you'd do it. As long as a stringified date-time is in MySQL's preferred format (yyyy-mm-dd hh:mm:ss), then it will be internally converted into a datetime value, and the comparisons will go ahead.
You'd only need the date/time functions you found if you want to do something more complicated than simple "greater/less than/equal" type comparison, e.g. "any records that have a December timestamp".
As Marc said, your code should work. But you probably want to do this programmatically with a variable for the time instead of the literal.
If you don't have the date-time specified as a string, but rather as a timestamp (e.g. from using the php time() function), then you can use the following query:
$query = "SELECT * FROM users WHERE FROM_UNIXTIME(" . $timestamp . ") <= lastupdated";
The key is the FROM_UNIXTIME() MySQL function.

Properly formatted MySQL date insert statement returns all 0's

I've created a PHP script that parses an HTML page, creates a MySQL query, and inserts the parsed data into a MySQL table. Everything works fine, except for one thing : the date field. When PHP runs the MySQL statement, the dates in the table read 0000-00-00 . If I echo the MySQL query, it results in the following :
INSERT INTO dispatch_table (dispatch_date, dispatch_time, dispatch1, dispatch2, dispatch3, dispatch4, dispatch5, dispatch6, dispatch7, dispatch8, dispatch9, dispatch10, dispatch11, dispatch12, dispatch13, dispatch14, dispatch15, dispatch16)VALUES ('2010-02-02', '10:46:17', '31.90' , '32.15','32.24','32.39','33.46','35.18','39.33','39.39','40.92','41.79','41.82','44.35','45.47','46.89','47.13','67.59');
If I copy and paste the statement into the MySQL table, it inserts the date just fine, but when PHP trys to insert it, I am given all 0's. The dispatch_date field is a "date" field in MySQL. Before inserting the dates with the PHP code, the date to insert is converted to a string (I thought this might solve my problem). Can anyone give me any insight on this?
From tizag.com
When you enter dates that are out of
the range or in the wrong format for a
given date type, MySQL will often just
enter in the default value of all
zeros.
You are storing them in the wrong format.
2010-02-02 (%Y-%m-%d) is the default format but it can be changed. Run this query to learn what the values are in your server:
SELECT ##date_format, ##datetime_format;
If they don't match, you'll have to either change your queries or change the ##date_format and ##datetime_format variables. They can be changed in a per-session basis:
SET ##session.date_format='%Y-%m-%d';
SET ##session.datetime_format='%Y-%m-%d %H:%i:%s';

Categories