By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

MySQL, time() and usinx dates Unix Style

P: n/a
Hi,

I have a field in my DB that saves the date/time as a integer.
I get the time on my server using the time() function.

now, moving away from php and looking at (My)SQL only.
If I use functions like DATE() or NOW(), I don't get a Unix value, (but
rather a formatted date).
So I cannot use those functions to check against my saved value.

How can I run a query that would get, say, 1 hour before the current time?

Many thanks in advance.

Simon

Jul 17 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
Sims <si*********@hotmail.com> wrote:
now, moving away from php and looking at (My)SQL only.
If I use functions like DATE() or NOW(), I don't get a Unix value, (but
rather a formatted date).
So I cannot use those functions to check against my saved value.

How can I run a query that would get, say, 1 hour before the current time?


Mysql had a fine online manual... There is a section dedicated to
date/time functions, you might want to read it.

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
now, moving away from php and looking at (My)SQL only.
If I use functions like DATE() or NOW(), I don't get a Unix value, (but
rather a formatted date).
So I cannot use those functions to check against my saved value.

How can I run a query that would get, say, 1 hour before the current
time?
Mysql had a fine online manual... There is a section dedicated to
date/time functions, you might want to read it.


Lets start small first, shall we?

Re-read my post first, the problem is in the time format that I would like
to use and not the manual.
BTW, Mysqql still has a fine online manual.

Thanks,

Simon
Jul 17 '05 #3

P: n/a
Sims <si*********@hotmail.com> wrote:
Mysql had a fine online manual... There is a section dedicated to
date/time functions, you might want to read it.


Lets start small first, shall we?

Re-read my post first, the problem is in the time format that I would like
to use and not the manual.


So you didn't bother to take a look at the date/time functions for
mysql!

I'll do your homework for you by providing this link:

Section 13.5 Date and Time Functions
<http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html>

Search for 'unix' and you'll find a couple of nice functions to
translate mysql dates to what you think you need.

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
>
I'll do your homework for you by providing this link:

Section 13.5 Date and Time Functions
<http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html>

Search for 'unix' and you'll find a couple of nice functions to
translate mysql dates to what you think you need.


I did look at the manual, (http://dev.mysql.com/doc/mysql/en/datetime.html),
but maybe, (just maybe), I did not find that particular page.

I don't know what I did wrong to you but maybe helping others is not your
forte.
Don't bother replying, obviously you will throw some form of insult and I am
too old for those games, (or to do homework for that matter).

Thanks for the help.

Simon
Jul 17 '05 #5

P: n/a
.oO(Sims)
I have a field in my DB that saves the date/time as a integer.


You should use the more reliable DATE or DATETIME instead. These are
native types and store dates in ISO format instead of Unix timestamps,
which allows

* to store much more dates (Unix timestamps are restricted in range)
* to use internal MySQL functions to work with these dates

Micha
Jul 17 '05 #6

P: n/a
Sims wrote:
I don't know what I did wrong to you but maybe helping others is not your
forte.

What you did was fail to RTFM even after being told that your solution
was in it, and what section to look in.

That demonstrates laziness and a desire for others to do your work for
you. That doesn't generally rally the troops to help you out.

You should apologize to Daniel.

Brian Rodenborn
Jul 17 '05 #7

P: n/a


What you did was fail to RTFM even after being told that your solution
was in it, and what section to look in.
No, what I failed to do was to read the _whole_ manual. Sorry if I am
neither a MySQL developer nor a DBA in general.
I was under the impression that the section I looked at was enough.

Re-read the thread, you seem to have misunderstood some/all of it so far.

The problem with search words like 'Date' 'Time' is that that they tend to
send you in many useless directions.
I could not find a answer in the doc so I asked here. Been told to read the
whole manual is a comical answer at best.

That demonstrates laziness and a desire for others to do your work for
you. That doesn't generally rally the troops to help you out.

You should apologize to Daniel.
Maybe I should, or maybe you are been a bit over-dramatic. But maybe you
should mind your own business.
He gave me a reply that I thought was not very useful and borderline rude,
if he took offence to my reply he can tell me about it himself.

But as I said it has nothing to do with you.
Brian Rodenborn


Simon.
Jul 17 '05 #8

P: n/a
I have a field in my DB that saves the date/time as a integer.


You should use the more reliable DATE or DATETIME instead. These are
native types and store dates in ISO format instead of Unix timestamps,
which allows

* to store much more dates (Unix timestamps are restricted in range)
* to use internal MySQL functions to work with these dates


Thanks, I'll have a look at it. I never really thought about it. I am used
to the C version of time and it's limitations.

When you say 'more reliable' are you talking about the restricted range of
the date or simply that it is less prone to errors in the database itself?

Sims
Jul 17 '05 #9

P: n/a
.oO(Sims)
When you say 'more reliable' are you talking about the restricted range of
the date [...]


Yep. Unix timestamps are restricted to 1970-2038 or something like that
on most systems. It may be enough in some cases, but the native date
types of MySQL also allow you to do all different kinds of calculations
and modifications directly in the db/query without having to convert
from/to timestamps.

OTOH Unix timestamps are useful from time to time for some calculations
as well, but you have to take care of some issues like daylight savings
for example.

Micha
Jul 17 '05 #10

P: n/a
.oO(Sims)
Thanks, I'll have a look at it. I never really thought about it. I am used
to the C version of time and it's limitations.


In your case have a look at DATE_ADD()/DATE_SUB() and the INTERVAL
keyword.

http://dev.mysql.com/doc/mysql/en/Da...functions.html

Micha
Jul 17 '05 #11

P: n/a
Michael Fesser wrote:
.oO(Sims)

Thanks, I'll have a look at it. I never really thought about it. I am used
to the C version of time and it's limitations.

In your case have a look at DATE_ADD()/DATE_SUB() and the INTERVAL
keyword.

http://dev.mysql.com/doc/mysql/en/Da...functions.html

Micha


I guess one of the things newbies have to learn is how to use search engines
within a particular site or WWW wide. Daniel correctly pointed him to this
page, but apparently did not grasp the concept. So, here is a real life example
of the MANY ways to point the OP in the right direction.

mysql> select unixtime,
from_unixtime(unixtime) as ut,
from_unixtime(unixtime)-interval 1 hour as nt
from mytable;
+------------+---------------------+---------------------+
| unixtime | ut | nt |
+------------+---------------------+---------------------+
| 1090722138 | 2004-07-24 21:22:18 | 2004-07-24 20:22:18 |
| 1090715410 | 2004-07-24 19:30:10 | 2004-07-24 18:30:10 |
.....
+------------+---------------------+---------------------+

All of us are willing to help, but if someone has extreme difficulty in coming
up with a solution, even after being given a pointer to the appropriate docs, he
should begin to question whether or not he is in the right profession.

--
Michael Austin.
Consultant - Not Available.
Donations still welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #12

P: n/a
> >>Thanks, I'll have a look at it. I never really thought about it. I am
used
to the C version of time and it's limitations.

In your case have a look at DATE_ADD()/DATE_SUB() and the INTERVAL
keyword.

http://dev.mysql.com/doc/mysql/en/Da...functions.html

Micha


I guess one of the things newbies have to learn is how to use search

engines within a particular site or WWW wide. Daniel correctly pointed him to this page, but apparently did not grasp the concept. So, here is a real life example of the MANY ways to point the OP in the right direction.


Thanks for your valuable input.

BTW, I am not a newbie. I might be, in your vast, un-swimmable, pool of
knowledge, but... a newbie in general I am not.
I am not sure what I did wrong here but I am enough not of a newbie to know
that I cannot get out of it. I am in one of those 'witch hunt threads'.
Sorry I asked.

But for the record, the link, (eventually), given by Daniel was not about
the reliability of UNIX timestamps vs. MySQL Dates raised by Michael
(Fesser).
I just replied to Michael (Fesser) because he had a good point that I never
really noticed. Sorry I did not read the manual on that point either.
I simply did not know that the 'OP'<me> should acknowledge, while replying
to a message, that a wise guru had pointed him in the right direction.

And as I said somewhere else in that thread, the words "Date()" and "Time()"
are not, IMH(newbie?)O, really easy search words to use. But of course, I am
certain you can provide us with some better search words that would prove me
wrong in my easy,(silly?), example.

Last but not least, where did I say that my profession had anything to do
with MySQL? or PHP? or even with computers for that matter? sorry, is it a
Guru only NG? If it is please accept my deepest apologies.

Talking about profession... I wonder...,
http://validator.w3.org/check?uri=ht...dbasource.com/ or
maybe
http://validator.w3.org/check?uri=ht...donations.html
But what do I know really.

Thanks for all that replied. I will spend the next year or so reading the
whole MySQL doc as wisely advised.

Sims.

Jul 17 '05 #13

P: n/a
Sims wrote:


What you did was fail to RTFM even after being told that your solution
was in it, and what section to look in.
No, what I failed to do was to read the _whole_ manual. Sorry if I am
neither a MySQL developer nor a DBA in general.
I was under the impression that the section I looked at was enough.


Why didn't you check the section he told you to?

"There is a section dedicated to date/time functions, you might want to
read it."

He gave me a reply that I thought was not very useful and borderline rude,
if he took offence to my reply he can tell me about it himself.

But as I said it has nothing to do with you.


This is a public forum, it's just as much my business when people behave
badly, as you did, as anyone else's. You have no say whether I do or do
not reply to your messages.

If you want to have private conversations with people, there's email.

Brian Rodenborn
Jul 17 '05 #14

P: n/a
> Why didn't you check the section he told you to?

"There is a section dedicated to date/time functions, you might want to
read it."
And further down the thread you can see something along the line of

"I did look at the manual, <link>, but maybe, (just maybe), I did not find
that particular page."

In the contexts it means a lot.

But as I said it has nothing to do with you.


This is a public forum, it's just as much my business when people behave
badly, as you did, as anyone else's. You have no say whether I do or do
not reply to your messages.


And, if you do not mind, I will use this very public forum to tell you that
you do not know when a person should apologise neither do you know when
another should mind their own business or get involved.
But I somehow suspect you do mind...

And I could also try and explain how silly your defence "This is a public
forum..." followed by "You have no say whether I do..." sound but that would
be out of the scope of this thread, I think.

If you want to have private conversations with people, there's email.
No, there's face to face. Anything else is not private.
Google around for some very good psychological reports on that matter.

Brian Rodenborn


This is not what I wish to use this public forum for. So I will refrain from
replying to your repeated attempts to ... what ever your secret mission is I
guess.

Sims.
Jul 17 '05 #15

P: n/a
Sims wrote:
<much snippage>
And as I said somewhere else in that thread, the words "Date()" and "Time()"
are not, IMH(newbie?)O, really easy search words to use. But of course, I am
certain you can provide us with some better search words that would prove me
wrong in my easy,(silly?), example.
"date function" found it on the MySQL site for me... "date time unix" would
also have found it.

Last but not least, where did I say that my profession had anything to do
with MySQL? or PHP? or even with computers for that matter? sorry, is it a
Guru only NG? If it is please accept my deepest apologies.

Talking about profession... I wonder...,
http://validator.w3.org/check?uri=ht...dbasource.com/ or
maybe
http://validator.w3.org/check?uri=ht...donations.html
But what do I know really.
....nor did I say I was a web developer.

Did the information on the query I provided help you solve your issue?

I have been using PHP and MySQL for less than 6 months... My primary profession
is Systems (OpenVMS - preferred and Unix/Linux/NT if I have too..) and Database
(Oracle/RDBMS and OracleRdb) Admin.

MySQL is almost a database. I still would not bet my livelyhood or my business
on it. It is okay for a "hobbyist" type system or writing "proof-of-concept"
apps, but would choose to deploy it on a real database engine.

Thanks for all that replied. I will spend the next year or so reading the
whole MySQL doc as wisely advised.

You're welcome.
Sims.


--
Michael Austin.
Consultant - Not Available.
Donations still welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #16

P: n/a
>
"date function" found it on the MySQL site for me...
Yes, it found it along with 953 documents.
"date time unix" would
as well as 1403 docs.
also have found it.
I might be difficult but I hope you see my point.
Last but not least, where did I say that my profession had anything to do with MySQL? or PHP? or even with computers for that matter? sorry, is it a Guru only NG? If it is please accept my deepest apologies.

Talking about profession... I wonder...,
http://validator.w3.org/check?uri=ht...dbasource.com/ or maybe
http://validator.w3.org/check?uri=ht...donations.html But what do I know really.


...nor did I say I was a web developer.


grand, so we both agree that we can use this forum even if it is not our
profession.

Did the information on the query I provided help you solve your issue?
Are you referring to the SELECT statement? It was helpful but as you said
yourself I had already been pointed in the right direction.
If you are referring to the search string above I am afraid that +/-1000
results is not as helpful I would hope.
I have been using PHP and MySQL for less than 6 months... My primary profession is Systems (OpenVMS - preferred and Unix/Linux/NT if I have too..) and Database (Oracle/RDBMS and OracleRdb) Admin.

MySQL is almost a database. I still would not bet my livelyhood or my business on it. It is okay for a "hobbyist" type system or writing "proof-of-concept" apps, but would choose to deploy it on a real database engine.


Ok, I'll take your word for it. I am not familiar enough with any databases
to compare, (hence the reason I ask questions here from time to time).

But I have worked with all the big ones, (<read> Oracle, Sybase etc..), and
MySQL does the same job as the others.
But seen that I am not betting my life or even my job on it I don't really
mind what db I have.

Sims.
Jul 17 '05 #17

P: n/a
Sims wrote:
And, if you do not mind, I will use this very public forum to tell you that
you do not know when a person should apologise neither do you know when
another should mind their own business or get involved.
But I somehow suspect you do mind...
Depends on what you mean by "mind". Naturally, I don't mind that you
felt free to express yourself. I disagree with your statement.
And I could also try and explain how silly your defence "This is a public
forum..." followed by "You have no say whether I do..." sound but that would
be out of the scope of this thread, I think.
Please do.
No, there's face to face. Anything else is not private.
Google around for some very good psychological reports on that matter.
Email is about as private as you will get. Public postings are most
definitely not.
This is not what I wish to use this public forum for.
Irrelevant to how I want to use them.
So I will refrain from
replying to your repeated attempts to ... what ever your secret mission is I
guess.


I have no secret agenda. My agenda is quite obvious. I thought you
behaved like an ass towards the entire group and to one person in
particular. I thought your behavior was bad enough that you should
apologize.

Anything else I need to spell out for you?

Brian Rodenborn
Jul 17 '05 #18

P: n/a
> I have no secret agenda. My agenda is quite obvious. I thought you
behaved like an ass towards the entire group and to one person in
particular. I thought your behavior was bad enough that you should
apologize.

Anything else I need to spell out for you?
Insulted the group? behaved like an arse?
hush little girl you're getting a nose bleed, sorry I was nasty to your
special "friend".


Brian Rodenborn


Sims
Jul 17 '05 #19

P: n/a
It seems to be a bit offtop
Jul 17 '05 #20

P: n/a
Sims wrote:
I have no secret agenda. My agenda is quite obvious. I thought you
behaved like an ass towards the entire group and to one person in
particular. I thought your behavior was bad enough that you should
apologize.

Anything else I need to spell out for you?
Insulted the group? behaved like an arse?


I said ass. As in donkey.
hush little girl you're getting a nose bleed, sorry I was nasty to your
special "friend".

Awwww, he thinks he knows some insults. He thinks by calling me a girl
or implying I'm gay that I'll be offended. That seems to indicate that
he thinks either of those two types of person are somehow lesser beings.

As I don't, the "insults" ring pretty hollow. Do however, continue
demonstrating to the rest of the group that you aren't the sort of
person who deserved help.


Brian Rodenborn
Jul 17 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.