phpMyAdmin auto insert and display only date - mysql

I've got a table with a date-type field
browser transform: text/plain: dateformat
transform option: 0,'%d-%b-%Y','local'
When I execute my query it stores 01-Jan-1970 (default value) and on page it shows me 0000-00-00
What I want to do is to store in database and in page only the date and dateformat Y-m-d like 27.02.2016.

You've got a couple of things going on that I should address first.
The phpMyAdmin transform feature affects how you insert or view data from within phpMyAdmin only. It doesn't change how the data is stored internally with MySQL and it doesn't change how other applications interact with MySQL. So when you talk about displaying in your blog or storing in MySQL, those aren't affected by the transformations you've configured.
Next, you don't appear to be setting the post date, which means you're probably getting '0000-00-00 00:00:00' stored in the column. The exception would be if you allow NULL or set a default value. You can also get zeroes if you insert invalid dates.
The appropriate thing to is use the MySQL type and format the display on output -- either in SQL or in your application; I usually do it in my application. How to do that will depend on which programming language your application uses.
When inserting, you can use NOW() to insert the current time without having to compute it yourself.

Related

Update my existing datetime column to my respective timezone in mysql

I have a table named "students" where student information are stored. Last week, I added a column (type-datetime) to keep track students last login time.
It was working well when testing with localhost. So, I uploaded to hosting and after a few days, I noticed datetime are different with my local times. I called now() function in my code and it is inserting with server time (Seattle,USA Time). I tried to set it with my timezone.
SELECT ##session.time_zone;
SET time_zone = 'Asia/Rangoon';
SET time_zone = "+06:30";
SET ##session.time_zone = "+06:30";
although it is executed, it don't affect and inserting with server time like before.
My Question is how should I update my existing datetime value column to my respective timezone. Thanks and appreciating.
The best approach imho is to save all dates in UTC. Only at presentation time these times should be converted to the time and zone for the user.
Because you are using PHP, you can convert input into UTC and convert output to the desired timezone for presentation.
This makes it also easy and possible to show foreign visitors their own time on your website.

Displays the date in MySQL Workbench

How to change the display of dates in MySQL Workbench
Recorded as 16-01-2014 now
But is necessary to do so: 16-01-2014
You'll need to change your SELECT statements to get your dates rendered differently.
Change
SELECT datestamp
to
SELECT DATE_FORMAT(datestamp,'%d-%m-%Y')
MySQL Workbench is a client program that accesses MySQL database servers. You use the workbench to write queries to send to a MySQL server to get back data.
If you want your dates presented in a particular way, that's part of the queries you write. If you use the queries built in to the workbench, you can't change the date format it displays.
Don't give in to the temptation to change the data type of your date and time columns in your table to VARCHAR(), and then fill them with values in the format you prefer. If you do that, you'll lose the ability to search your table on date ranges.

Convert timezone for all MySQL queries for a specific user

I work on an existing project that stores dates to a MySQL DB. The dates are stored as UTC since all users so far were in GTM+0 and no conversion was needed.
I now need to modify the code so that users from other time zones can use the system. The users choose their timezone when they register to the system, so I have a table holding the timezone for each user.
I know I can use CONVERT_TZ() when I extract and store the dates, but to do so means to go through all the queries and add this function.
When I do:
SET ##session.time_zone:='+7:00';
select now();
The result changes with the timezone variable.
When I do:
SET ##session.time_zone:='+7:00';
select myDate from myTable;
The result stays the same, returning what is stored in the DB.
Is there any way I can change the connection string or is there a session variable I can use that will affect the queries without having to add CONVERT_TZ to every single query?
Edit: this is not a duplicate of Should I use field 'datetime' or 'timestamp'? since using timestamp means I need to change all the date field in the DB, while I try to change something more global so I will not have to do massive changes the Db fields or the code.

phpmyadmin export of date int(11) to CSV dd/mm/yy format

Im new to SQl and trying to do a dump through phpmyadmin.
At the moment date data is stored in my DB as int(11).
When I export from phpmyadmin, the data is naturally exported as numbers like '1325336400' but i would like this to display as 01/01/2012 or similar format. is there any way I can do this?
Many thanks in advance
Jus
If you're storing your "date data" (as you put it) in 32-bit integers, I guess you are using *nix timestamp values (seconds since the 1-jan-1970 00:00 UTC epoch).
(You know this may overflow sometime in 2038, right? http://en.wikipedia.org/wiki/Year_2038_problem)
phpmyadmin has a hard time with these values, as you have discovered.
MySQL has a TIMESTAMP data type which also uses *nix-style timestamps. (It won't overflow; the MySQL developers did the right thing.)
You really do need to convert your date data to the TIMESTAMP data type. Otherwise dealing with time will be a huge pain in the neck, forever. Here's how to do it.
First, add a column to your table in this way,
ALTER TABLE mytable ADD COLUMN ts TIMESTAMP AFTER myinttimestamp
Then populate your new ts column using the values you already have.
UPDATE TABLE mytable SET ts = FROM_UNIXTIME(myinttimestamp)
Next, change the definition of your new column so it disallows NULL values and uses the current time as a default:
ALTER TABLE mytable
CHANGE ts
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
Finally, if you want you can get rid of the old column with the INT values in it.
ALTER TABLE mytable DROP COLUMN myinttimestamp
(You should consider trying all this out on a copy of your table; it would stink to make a mistake and wreck your data).
When you use the TIMESTAMP data type, MySQL does its best to store all these timestamps internally in UTC (time-zone-insensitive) time, and convert them to local time upon display, based on how you set
SET time_zone = 'Asia/Vladivostok'
or whatever. It will also convert them from local time to UTC time when you put them in to the data base.
Here's a write up.
https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

No datetime values populated from MySQL using SubSonic 3 Linq

I have a MySQL table with a couple of Datetime columns. The columns are set to allow null and some have default value '0000-00-00 00:00:00'. This is a conversion project from ASP to ASP.NET so the table is full of data, and where some rows still have the default value, so I had to set "Allow Zero Datetime=True" in the connectionstring to avoid the exception "Unable to convert MySQL date/time value to System.DateTime"
Now when I generate the code it all works fine and I get properties of type DateTime? for those columns, but when I query the database and populate an object representing the table all DateTime properties are null. Other properties gets populated their correct values.
Anybody knows why?
I'm using MySQL Connector 6.1.3 and SubSonic.Core compiled from the github today (11/17/2009)
I did some data cleaning. Updated all datetime columns to null where date was '0000-00...' and removed "Allow Zero Datetime=True" from the connectionstring, and then it works. Guess zero dates are not supported by SubSonic, and why should it, I donĀ“t see any use for zero dates over null.
My trick for converting datetime format from different SQL DBMS is to load the column as VARCHAR. Then use string functions such as SUBSTRING and CONCAT to play around and get the desire format. From experience this saves a lot of time. No need to worry about dbms automatic conversion for datetime.
MySQL uses 'YYYY-MM-DD HH:MM:SS'