| | |
Converting int(10) to Date Format
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
I am very new to this so I apologize if I get confusing with my post.
I have a database that I access through CPanel then through phpMyAdmin. I am running a query to find out when the last login was made by users and I am getting values such as '1233345786' returned.
I need to be able to convert this integer value to a date value of 'MM/dd/yyyy'. When I try the field is returning NULL as the value. When I try Convert_Date the query returns an error saying my table does not contain this function.
And just to clarify further, I can see the correct dates when viewed via my PHP plugin but when I run it via SQL from the database backend that is when I am getting my error. I need to be able to see this from the backend as well because I believe my PHP code for the plugin that I created is flawed.
Can anyone help? I am running out of options.
I have a database that I access through CPanel then through phpMyAdmin. I am running a query to find out when the last login was made by users and I am getting values such as '1233345786' returned.
I need to be able to convert this integer value to a date value of 'MM/dd/yyyy'. When I try
PHP Syntax (Toggle Plain Text)
SELECT date_format(`user_lastvisit`, '%d %m %Y')
And just to clarify further, I can see the correct dates when viewed via my PHP plugin but when I run it via SQL from the database backend that is when I am getting my error. I need to be able to see this from the backend as well because I believe my PHP code for the plugin that I created is flawed.
Can anyone help? I am running out of options.
Last edited by jugg3r; Jun 9th, 2009 at 1:47 pm.
Why not get the 'user_lastvisit' data and place it in a variable, e.g. $last and then do this:
Is that too much of a mess?
$last_visit = date($last,'d m Y') Is that too much of a mess?
Last edited by ardav; Jun 9th, 2009 at 2:54 pm.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
I could do that if I was trying to get the PHP page to display the date. I already have that working with a Convert_Date function. I am trying to run a SQL Query inside the database itself via myPHPAdmin and get the Integer value converted to date.
Hi.
This integer date thing is a Unix timestamp, I assume?
If so, check out FROM_UNIXTIME
If not, how exactly is it formatted?
P.S.
You should ALWAYS store dates and times in the standard date/time format (YYYY-MM-DD HH:SS), and store them in date type fields, to avoid problems just like this one.
This integer date thing is a Unix timestamp, I assume?
If so, check out FROM_UNIXTIME
If not, how exactly is it formatted?
P.S.
You should ALWAYS store dates and times in the standard date/time format (YYYY-MM-DD HH:SS), and store them in date type fields, to avoid problems just like this one.
Last edited by Atli; Jun 9th, 2009 at 5:01 pm. Reason: Added the P.S.S
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Hi.
This integer date thing is a Unix timestamp, I assume?
If so, check out FROM_UNIXTIME
If not, how exactly is it formatted?
P.S.
You should ALWAYS store dates and times in the standard date/time format (YYYY-MM-DD HHS), and store them in date type fields, to avoid problems just like this one.
I appreciate all of your help.
•
•
•
•
Hi.
You should ALWAYS store dates and times in the standard date/time format (YYYY-MM-DD HHS), and store them in date type fields, to avoid problems just like this one.
A database is a machine construct designed for the efficient handling of data by a logical machine
Best practice is to store a single 10 digit unix timestamp in the database
there is no "standard date/time format (YYYY-MM-DD HHthe unix timestamp is easily selectable for date ranges, before after or any comparison because it is solely numericS)" every user/region has a different date time standard.
12/01/08 which could be
12 January 2008depending on user location
1 December 2008
8 January 2012
a 10byte timestamp is smaller than a 50byte text representation which is important as the database grows (500million date strings)
The data is stored correctly, and should only be formatted on output, to the preference of the viewer depending on local custom.
This is important in database optimization
globalization and
localization
Last edited by almostbob; Jun 9th, 2009 at 5:55 pm.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
•
•
Join Date: Jul 2008
Posts: 148
Reputation:
Solved Threads: 25
•
•
•
•
P.S.
You should ALWAYS store dates and times in the standard date/time format (YYYY-MM-DD HHS), and store them in date type fields, to avoid problems just like this one.
So I completely DISAGREE with always storing date time information in a textual fashion. Both have their place, both advantages and disadvantages.
^ beat me to the punch. What he said
Last edited by mschroeder; Jun 9th, 2009 at 5:54 pm. Reason: late to the party
If you're question/problem is solved don't forget to mark the thread as Solved!
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
-- Code I post is usually but not always tested. If it is tested it will be against 5.2.11 or 5.3.0
•
•
•
•
I could do that if I was trying to get the PHP page to display the date. I already have that working with a Convert_Date function. I am trying to run a SQL Query inside the database itself via myPHPAdmin and get the Integer value converted to date.
date('d m Y', $last) Using Atli's suggestion, try this:
PHP Syntax (Toggle Plain Text)
SELECT FROM_UNIXTIME(user_lastvisit,'%d %m %Y');
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Glad it worked, jugg3r :)
@mschroeder and almostbob
I'm not saying you shouldn't use timestamps, but not in an integer field. You can, obviously, but MySQL doesn't consider it a date, which, unconverted, excludes it from much of (if not all of) the built in MySQL date handling functionality, which can cause confusion (as evident by this thread).
And I did NOT, nor will I ever, recommend strong dates as text.
If you think that is what happens when you use the built in date types... you must not think much of the MySQL devs.
Allow me to recite the highlights of 10.3.1. The DATETIME, DATE, and TIMESTAMP Types, from the manual.
MySQL has a special TIMESTAMP data type, which is a 4 byte MySQL specific version of a Unix-like timestamp . If you plan on storing a timestamp, this would be the type to use.
Note that this is not the exact same thing as a Unix timestamp, but it seems to have the same range and is it is comparable, if not smaller, in size.
Also note that if you need a Unix timestamp output, you can still use this type, including all the MySQL features, and convert it when you output it.
The other two, DATE and TIME are both 4 bytes (integer packages), and the DATETIME type is a 8 byte combo of both.
They are NOT 50byte string representation of the data they store.
The advantage with these is that they can store almost an indefinitely high date (year 1000 - 9999), whereas a Unix timestamp, and the MySQL timestamp, can only go as high as 2039-01-09 (and change).
When I say the format "YYYY:MM:DD HH:MM:SS" is the "standard", I am not talking about any sort of localized regional nonsense.
This is the format MySQL prefers, even with the TIMESTAMP type, and this is the format MySQL will always return DATETIME values to you, regardless of whatever localization you use.
This is why I advice using the "standard" format with the built in date type functions and functionality. It makes little sense to not take advantage of that by storing the date data as a number. (Unless you have a compelling reason not to.)
Did I forget something?
I'll come back to it... got to run.
@mschroeder and almostbob
I'm not saying you shouldn't use timestamps, but not in an integer field. You can, obviously, but MySQL doesn't consider it a date, which, unconverted, excludes it from much of (if not all of) the built in MySQL date handling functionality, which can cause confusion (as evident by this thread).
And I did NOT, nor will I ever, recommend strong dates as text.
If you think that is what happens when you use the built in date types... you must not think much of the MySQL devs.
Allow me to recite the highlights of 10.3.1. The DATETIME, DATE, and TIMESTAMP Types, from the manual.
MySQL has a special TIMESTAMP data type, which is a 4 byte MySQL specific version of a Unix-like timestamp . If you plan on storing a timestamp, this would be the type to use.
Note that this is not the exact same thing as a Unix timestamp, but it seems to have the same range and is it is comparable, if not smaller, in size.
Also note that if you need a Unix timestamp output, you can still use this type, including all the MySQL features, and convert it when you output it.
The other two, DATE and TIME are both 4 bytes (integer packages), and the DATETIME type is a 8 byte combo of both.
They are NOT 50byte string representation of the data they store.
The advantage with these is that they can store almost an indefinitely high date (year 1000 - 9999), whereas a Unix timestamp, and the MySQL timestamp, can only go as high as 2039-01-09 (and change).
When I say the format "YYYY:MM:DD HH:MM:SS" is the "standard", I am not talking about any sort of localized regional nonsense.
This is the format MySQL prefers, even with the TIMESTAMP type, and this is the format MySQL will always return DATETIME values to you, regardless of whatever localization you use.
This is why I advice using the "standard" format with the built in date type functions and functionality. It makes little sense to not take advantage of that by storing the date data as a number. (Unless you have a compelling reason not to.)
Did I forget something?
I'll come back to it... got to run.
![]() |
Similar Threads
- Want Date Format in dd/mm/yyyy in ASP.Net With C#. (C#)
- Excel vba 6.0 copy date cells date format (Visual Basic 4 / 5 / 6)
- reading date format (Python)
- Change date format (Oracle)
- Date format in MS Office 2000 (Visual Basic 4 / 5 / 6)
- VBA Date Format (Visual Basic 4 / 5 / 6)
Other Threads in the PHP Forum
- Previous Thread: Export PHP Result To Excel 2003
- Next Thread: suggetion
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube






S), and store them in date type fields, to avoid problems just like this one.