Date being changed when updating MySQL field - php

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.

Related

Cant update Timestamp

I am having problem with my MySQL database.
I have 4 column id, user, msg, time.
Type of time is "timestamp",
Attributes is " ON UPDATE current_time_stamp"
but it always storing 0000-00-00 00:00.
I want to store it current date and time please help
I really am not able to understand what is wrong in it. As an alternative, you try to get the current time using date() function of php and store it in your database table.
$dt=date('h:i:s:a')
Will give you the current time in hour:minute:second:am/pm format.
To get date also, you can use:
$dt=date('y-m-d h:i:s:a')
Hope it will help you.

Can month and day values in a PHP and MYSQL DATE variable be 0 or null?

I'm in the planning stages of creating a historical database. I will be using PHP, MYSQL and JavaScript for the website.
Often someone will know what year a person was born or picture was taken, but not the month or the day.
Is it possible for a PHP DATE variable and MYSQL DATE to be:
1920-00-00 or 1845-12-00 ?
If not, unless someone has a better idea, I'll have to create a column for year, another column for month, and yet another for day then do a bunch of value checking and combining.
Thoughts?
Thanks.
You need exakt dates for using date in mysql, so you probably have to bite down and filter everything for validating and then some code to combine it.

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.

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.

MySQL DATE_ADD - date is wrong?

So I've got a simple query in MySQL that sets a new member's expiration date once they pay their dues:
UPDATE members SET joined=CURDATE(), expires=DATE_ADD(CURDATE(), INTERVAL 1 YEAR), active='1' WHERE id=1000
this query has run 200+ times, normally with the correct result - the current date is put in the joined field, and a year from that date in the expires field. However, in about 10 instances, the expires date has been set to 00-00-0000 with no obvious explanation. I started writing the query to a text file every time to make sure the syntax was correct and I hadn't missed anything - and I didn't - it's exactly that query (with only the ID varying) for every query, those that work, and those that don't.
The only thing I can think here is that there must be an issue with MySQL's DATE_ADD function. Has anyone else experienced anything like this?
UPDATE:
I should add that the joined field is correct with the current date in the cases where the expires date is incorrect.
I'm using MySQL 5.0.81.
There are no triggers.
The table is using MyISAM.
IMPORTANT UPDATE:
I'm an idiot - when I say 11-30-1999 that's not actually what's in the database. I absent-mindedly wrote that, but in fact the database contains the value 00-00-0000 - 11-30-1999 is just how it gets rendered by PHP onto my page. Sorry about that, hopefully that will make this problem less difficult to figure out.
Just a thought.... those "wrong" dates didn't happen to be leap year dates did they.... Feb. 29th for example?
It shouldn't matter, but it may be a bug.

Categories