473,508 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Datetime within past week

I have a series of records in a database. When each record is stored,
the datetime is logged: $date = date("Y-m-d H:i:s");

Prior to adding a new record to the database, I want to run a query to
retrieve all of the records uploaded in the last 7 days. I thought it
would be easiest to:

Pseudo Code:
$newDate = $date - 7 days;
select * from TABLE where DATE $newDate;

Problem is I can't figure out how to subtract 7 days from $date and
convert that value to a valid datetime object.

Thanks for any help

Nov 19 '06 #1
4 5492
Greg Scharlemann wrote:
I have a series of records in a database. When each record is stored,
the datetime is logged: $date = date("Y-m-d H:i:s");

Prior to adding a new record to the database, I want to run a query to
retrieve all of the records uploaded in the last 7 days. I thought it
would be easiest to:

Pseudo Code:
$newDate = $date - 7 days;
select * from TABLE where DATE $newDate;

Problem is I can't figure out how to subtract 7 days from $date and
convert that value to a valid datetime object.

Thanks for any help
You can do it in SQL. Assuming you're using MySQL look for SUBDATE() on
the following page:
http://dev.mysql.com/doc/refman/5.0/...functions.html

If you want to do it in PHP you can do eg:
date('Y-m-d H:i:s', time() - 86400 * 7);

time() returns the current timestamp. There are 86400 seconds in a day
so multiplying that by 7 gives you the seven days you're after. Then
use date() to format and put into your sql statement.

It's also possible to do it with mktime() eg:
$timestamp = mktime(0, 0, 0, date('m'), date('d')-7, date('Y'))
although there are more function calls doing it this way.
See http://www.php.net/mktime for more details

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Nov 19 '06 #2
Greg Scharlemann wrote:
I have a series of records in a database. When each record is stored,
the datetime is logged: $date = date("Y-m-d H:i:s");

Prior to adding a new record to the database, I want to run a query to
retrieve all of the records uploaded in the last 7 days. I thought it
would be easiest to:

Pseudo Code:
$newDate = $date - 7 days;
select * from TABLE where DATE $newDate;

Problem is I can't figure out how to subtract 7 days from $date and
convert that value to a valid datetime object.

Thanks for any help
If it's MySQL, you can do it all in SQL:

$result = mysql_query('SELECT myColumn FROM myTable where datacol=
SUBDATE(NOW(), INTERVAL 7 DAY))';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 19 '06 #3

Chris Hope wrote:
>
If you want to do it in PHP you can do eg:
date('Y-m-d H:i:s', time() - 86400 * 7);
Exactly what I was looking for! Thanks!

Nov 19 '06 #4
Following on from Chris Hope's message. . .
>If you want to do it in PHP you can do eg:
date('Y-m-d H:i:s', time() - 86400 * 7);
To be pedantic this isn't "last 7 days". It could be important if say a
customer makes a regular order. Suppose last Monday they managed to put
their order in at 11am but this Monday don't get round to it until
11:30am; now 'Last weeks' order won't appear in the list. To the
customer this could be confusing when last weeks order sometimes appears
and sometimes doesn't.

* It may not matter, so no need to be more complicated
* If it does then even an 'exact same time' or '(86400*7)+(60*30) could
fail when switching to daylight saving time.
* It isn't difficult to establish what '00:00:01' is when you know the
page in the manual, but once again things can go wrong at daylight
saving and beware the simple coding error.
>It's also possible to do it with mktime() eg:
$timestamp = mktime(0, 0, 0, date('m'), date('d')-7, date('Y'))
although there are more function calls doing it this way.
See http://www.php.net/mktime for more details
--
PETER FOX Not the same since the borehole business dried up
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Nov 20 '06 #5

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

Similar topics

14
6403
by: Paul Moore | last post by:
I was just writing some code which did date/time manipulations, and I found that the Python 2.3 datetime module does not supply a number of fairly basic functions. I understand the reasoning (the...
1
8792
by: Michael Howes | last post by:
If I have two integers, a week and year how can I create a DateTime object that is set to the first day of the week of that week/year? If I have two integers, a quarter and a year, how can I...
7
7476
by: .Net Sports | last post by:
Before processing my data in a datagrid, I need to parse the day of the week (which will be my 'rqsday' variable) from a string that comes over on a querystring: string rqs =...
6
8964
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
13
4151
by: Alan M Dunsmuir | last post by:
I need to specify a new data type, almost entirely a 'clone' of the existing DateTime type, with the following specific difference, and all the consequent differences in properties and methods...
2
1906
by: scott.swank | last post by:
Hello all. I'm running SQL Server 2000 and I'm trying to get a very few, recent rows of data from a table based on an indexed datetime column. Here's my predicate: where order_date >...
8
4618
by: ajs | last post by:
I have a process that verifies that a server is running. If the server stops responding I send an email to a support person. Now I want to add a downtime indicator so that my code does not send...
5
2582
by: iulian.ilea | last post by:
Is correct to have a varchar field and insert dates of type dd/mm/yyyy into it? I choose this method because I have an application that runs on more than one server. So, if I used a datetime field...
0
16475
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7336
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
7401
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...
1
7063
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...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.