473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Date conversions

I have a database that stores time in 2 different formats. I have a
unix timestamp, and the other is a representation of time in the
format 03:00:00 which would represent 3 hrs. I want to convert the 3
hrs so that I can add it to the unix timestamp. In other words,

The unix timestamp of 1183553400 is equal to Wed, Jul 04, 2007 7:50
AM. I want to add the 3hrs to be able to display in this format

Wed, July 04, 2007 7:50 AM - 10:50 AM.
Thanks.

Jul 16 '07 #1
4 1653
..oO(mtuller)
>I have a database that stores time in 2 different formats. I have a
unix timestamp, and the other is a representation of time in the
format 03:00:00 which would represent 3 hrs. I want to convert the 3
hrs so that I can add it to the unix timestamp. In other words,
Why not simply store the date in a real DATETIME field? You can always
use UNIX_TIMESTAMP() if you need it. Then all you need would be to call
ADDTIME() to add both values.

If converting the column is not an option, use FROM_UNIXTIME() before
calling ADDTIME().

HTH
Micha
Jul 16 '07 #2
Rik
On Mon, 16 Jul 2007 20:35:11 +0200, mtuller <mi******@gmail.comwrote:
I have a database that stores time in 2 different formats. I have a
unix timestamp, and the other is a representation of time in the
format 03:00:00 which would represent 3 hrs. I want to convert the 3
hrs so that I can add it to the unix timestamp. In other words,

The unix timestamp of 1183553400 is equal to Wed, Jul 04, 2007 7:50
AM. I want to add the 3hrs to be able to display in this format

Wed, July 04, 2007 7:50 AM - 10:50 AM.
You made it somewhat difficult for yourself.
Here is how it would be done in MySQL alone (untested, fields named 'date'
and 'time'):

SELECT
CONCAT(
DATE_FORMAT(
FROM_UNIXTIME(`date`),
'%a, %M %D, %Y %r'
),
' - ',
DATE_FORMAT(
DATE_ADD(
FROM_UNIXTIME(`date`), INTERVAL TIME_TO_SEC(`time`) SECOND
),
'%r'
)
) as 'formatted_date'
FROM `tablename`;

Which will still give you grief if it crosses the 24:00:00/00:00:00
threshold.
--
Rik Wasmus
Jul 16 '07 #3
Rik <lu************@hotmail.comwrote in
news:op.tvkoaxivqnv3q9@metallium:
On Mon, 16 Jul 2007 20:35:11 +0200, mtuller <mi******@gmail.com>
wrote:
>I have a database that stores time in 2 different formats. I have a
unix timestamp, and the other is a representation of time in the
format 03:00:00 which would represent 3 hrs. I want to convert the 3
hrs so that I can add it to the unix timestamp. In other words,

The unix timestamp of 1183553400 is equal to Wed, Jul 04, 2007 7:50
AM. I want to add the 3hrs to be able to display in this format

Wed, July 04, 2007 7:50 AM - 10:50 AM.

You made it somewhat difficult for yourself.
Here is how it would be done in MySQL alone (untested, fields named
'date' and 'time'):
whoever uses the code would probably wise to use differently named fields -
'date' and 'time', while they work, should probably be avoided.

http://dev.mysql.com/doc/refman/4.1/...ved-words.html

Jul 16 '07 #4
Rik
On Mon, 16 Jul 2007 22:52:23 +0200, Good Man <he***@letsgo.comwrote:
Rik <lu************@hotmail.comwrote in
news:op.tvkoaxivqnv3q9@metallium:
>On Mon, 16 Jul 2007 20:35:11 +0200, mtuller <mi******@gmail.com>
wrote:
>>I have a database that stores time in 2 different formats. I have a
unix timestamp, and the other is a representation of time in the
format 03:00:00 which would represent 3 hrs. I want to convert the 3
hrs so that I can add it to the unix timestamp. In other words,

The unix timestamp of 1183553400 is equal to Wed, Jul 04, 2007 7:50
AM. I want to add the 3hrs to be able to display in this format

Wed, July 04, 2007 7:50 AM - 10:50 AM.

You made it somewhat difficult for yourself.
Here is how it would be done in MySQL alone (untested, fields named
'date' and 'time'):

whoever uses the code would probably wise to use differently named
fields -
'date' and 'time', while they work, should probably be avoided.

http://dev.mysql.com/doc/refman/4.1/...ved-words.html

Absolutely right, allthough one get around around reserved words quite
easily, it should be more descriptive. However, the question was not how
to set up the database (which could be better) or name the fields. As I
haven't got a clue what the actual meaning of the data is, I really am not
the one to come up with alternative names :-).

--
Rik Wasmus
Jul 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
by: Mick White | last post by:
According to the Safari browser the world began on "Fri Dec 13 1901 15:45:52 GMT-0500", but I need to be able to get around this limitation. I am interested in dates from 1500 to 1901, as far as...
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
8
by: John Wildes | last post by:
Hello all I'm going to try and be brief with my question, please tell me if I have the wrong group. We are querying transaction data from a DB3 database application. The dates are stored as...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
4
by: blini | last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need to convert and to compare if "09/06/2006 14:20" is lesser or equal that the current date. Everything in Javascript.
7
by: Richiep | last post by:
I am trying to get a UK format date of dd/mm/yyyy. Why does the following subroutine not return a valid date in a web form? The date returned is #12:00:00 AM# but the date I entered into the...
9
by: Bob Sanderson | last post by:
I have a field in a database called DateRcvd. At present, it outputs in my report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy format. What is the easiest way to accomplish...
10
by: DontellTrevell via AccessMonster.com | last post by:
HELP!!....I need to calculate the numer of days elapsed between two field. But, the date format is YYYYMMDD. How can i accomplsh this? -- Dontell Trevell Message posted via AccessMonster.com...
6
by: Jeremy Sanders | last post by:
Hi - I need to add support to a program for dates and times. The built-in Python library seems to be okay for many purposes, but what I would like would be Unix epoch style times (seconds relative...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.