Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
mySQL datetime, PHP Date() and arithmatic
Old 06-17-2008, 07:13 AM mySQL datetime, PHP Date() and arithmatic
doodler's Avatar
Novice Talker

Posts: 8
Location: UK
Trades: 0
Hello,
This should be a relatively easy one (I think).

In PHP, how do you say now minus 2.5 minutes (or rather 2.5 minutes ago)?

It would also be incredibly helpful if someone could point out how its possible to say within x amount of minutes, e.g.
$query = "SELECT * FROM table WHERE name = 'jack' and born = {within the last 15 minutes} Limit 0,1"

Any help & advice would be appreciated.
__________________
doodler [
Please login or register to view this content. Registration is FREE
]
doodler is offline
Reply With Quote
View Public Profile Visit doodler's homepage!
 
 
Register now for full access!
Old 06-17-2008, 07:30 AM Re: mySQL datetime, PHP Date() and arithmatic
Arenlor's Avatar
Ultra Talker

Posts: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
For the PHP time() - (150); is 2.5 minutes ago. You substract seconds. You can also use math, time() - (2.5 * 60); is 2.5 minutes ago also. That makes it easier for larger values (such as days or years even). For the sql you just use >.
PHP Code:
$fifteen_ago time() - (15 60);
$query "SELECT * FROM table WHERE name='jack' and born > $fifteen_ago LIMIT 0,1";
//This is assuming that baby jack was inserted with a now() fifteen minutes ago or sooner. 
I may be too tired to be programming at the moment, so it may be < not >
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 06-17-2008, 07:34 AM Re: mySQL datetime, PHP Date() and arithmatic
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
In mysql:
Code:
SELECT * 
FROM table 
WHERE name = 'jack' 
and born between now()-interval 15 minute and now()
Limit 0,1
Code:
select now()-interval 15 minute as past, now() as present;
+---------------------+---------------------+
| past                | present             |
+---------------------+---------------------+
| 2008-06-17 13:18:28 | 2008-06-17 13:33:28 |
+---------------------+---------------------+
1 row in set (0.00 sec)
It's that simple in mysql 5. Cehck the documentation for older versions, you might need to use date_add() or date_sub().
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-17-2008, 07:37 AM Re: mySQL datetime, PHP Date() and arithmatic
doodler's Avatar
Novice Talker

Posts: 8
Location: UK
Trades: 0
Appreciate the responses, I'll test them out in about 5 hours time.

tripy, is that mysql the actual code to use?! I'm a bit stunned how english-friendly that syntax is!
I'm no expert in SQL hence why I'm struggling on this issue.

Last edited by doodler; 06-17-2008 at 07:39 AM..
doodler is offline
Reply With Quote
View Public Profile Visit doodler's homepage!
 
Old 06-17-2008, 07:37 AM Re: mySQL datetime, PHP Date() and arithmatic
Arenlor's Avatar
Ultra Talker

Posts: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
How I've forgotten the mysql CLI
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 06-17-2008, 07:41 AM Re: mySQL datetime, PHP Date() and arithmatic
doodler's Avatar
Novice Talker

Posts: 8
Location: UK
Trades: 0
I should also point out that values inserted into the `born` field are done by using NOW() and the field-type is DATETIME right to the second (e.g. 2008-06-17 12:41:50).
__________________
doodler [
Please login or register to view this content. Registration is FREE
]
doodler is offline
Reply With Quote
View Public Profile Visit doodler's homepage!
 
Old 06-17-2008, 07:42 AM Re: mySQL datetime, PHP Date() and arithmatic
doodler's Avatar
Novice Talker

Posts: 8
Location: UK
Trades: 0
Quote:
Originally Posted by Arenlor View Post
How I've forgotten the mysql CLI
Erm, "CLI"?
__________________
doodler [
Please login or register to view this content. Registration is FREE
]
doodler is offline
Reply With Quote
View Public Profile Visit doodler's homepage!
 
Old 06-17-2008, 08:05 AM Re: mySQL datetime, PHP Date() and arithmatic
Arenlor's Avatar
Ultra Talker

Posts: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
CLI is command line interface, his second quote is from it.
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 06-17-2008, 09:27 AM Re: mySQL datetime, PHP Date() and arithmatic
doodler's Avatar
Novice Talker

Posts: 8
Location: UK
Trades: 0
Hey guys,

I've just tested the "BETWEEN now( ) - INTERVAL 2 MINUTE AND now()" SQL code and seems to work OK (that is, the result is as expected).

Seems I've learnt something new about SQL today

Thanks a lot.
doodler is offline
Reply With Quote
View Public Profile Visit doodler's homepage!
 
Old 06-17-2008, 04:31 PM Re: mySQL datetime, PHP Date() and arithmatic
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
tripy, is that mysql the actual code to use?! I'm a bit stunned how english-friendly that syntax is!
Well, yes, as you have already seen.
SQL is not that hard, and when dealing with datas coming from a DB, you can simplify your PHP code a lot by using the DB included functions.
The hard part is that it's a language with a semantic and an orientation that's not similar to the usual (read PHP, python, ruby, C#, java and almost anything).
Hence so many peoples are uncomfortable with it.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to mySQL datetime, PHP Date() and arithmatic
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32998 seconds with 12 queries