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.
Related
I need to be able to set a Null value in DB Oracle if the user wont choose a date.
$createdon = date('m/d/Y', strtotime($_POST['createdon']));
INSERT INTO purchase (CREATEDON) VALUES(to_date('$createdon','mm/dd/yy'))
I am able to insert date if a user choose a date but if the user wont the date that is save on the database is "01/01/1970". Also, if i dont use "to_date" tag i cant insert record on the database.
Any suggestion on what should i do and how?
Thank you.
TO_DATE is OK because - if you're passing a string (which represents a date value), it is a preferable way so that YOU have control over it. Because, if you pass '01.02.20', which is which? Is 01 day or month? Is 02 day, year, ... month? Who knows. Don't depend ond defaults. So, if you say to_date('01.02.20', 'rr.mm.dd'), then everyone (Oracle included) knows what to do.
As of 01/01/1970: if you insert null but still see that value in a column, it means that
column has a default value
database trigger inserted it
Check both of these on that table.
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!
I'm running into a weird issue where I'm posting a date in Y-m-d format yet it's being changed to a completely different date once I view in the actual MySQL table.
Here's the query
UPDATE $admins_table
SET expire=$expireu
WHERE identity='$donation_row[steam_id]
The expire field is what I'm having issues with. The field itself is a varchar, and the $expireu variable is always a date in Y-m-d format ex. 2013-11-16
When that query is run, with the date I gave as an example above, I get a weird result in the actual MySQL table. If I go to view the table, instead of it storing 2013-11-16 it has stored 1986 as the date. No month or day, just 1986.
I may have made a very stupid/silly mistake, but at this point I'm unsure of what I've flubbed. Any help in the right direction would be much appreciated, thank you.
haha, use quotes!
UPDATE $admins_table SET expire='$expireu' WHERE identity='$donation_row[steam_id]'
mysql substracts 2013-11-16 == 1986
the use of ' and " are your friends. you are passing a math problem into mysql which it is solving and then saving the result of. wrap that date in quotes.
I am unsure on how to check whether a date has passed in order to only show rows where the date has not passed.
I am using CodeIgniter and when the date is set, I'm not using a date type (using varchar) in the MySQL table as I'm using the jQuery date-picker to set the date.
So when the date is set, I need to pull the date from the table to check whether the date has passed or not?
So I'm not sure whether I have to totally change my date input to use the date type in the MySql table in order to pull the date and check whether it is < now()
Any guidance or advice would be extremely appreciated :)
Is there any particular reason for NOT using the date type in MySQL? If is a date, why not store it as a date? It will still be usable as a string in your PHP script when you retrieve it from the database.
You should always use the date type in MySQL if you are tracking dates, that way you have all the date functions available to you. The date functions of MySQL are much faster than trying to parse them as text with PHP.
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.