Jump to content

Entries from last 7 days


adzie

Recommended Posts

Hello all  :)

 

I'm hitting a brick wall here so I'm hoping you can help.

 

I can get my script to count all the entries and give the result, but I have been unable thus far to get it to display the entries from only the last 7 days.  The date format in the db is  as an example

 

20060422081640

 

I'd appreciate any help.

 

many thanks

 

$query7 = "SELECT * FROM records WHERE member =\"".$member."\"";
$result7 = mysql_query($query7);
$number7 = mysql_numrows($result7); 

Link to comment
Share on other sites

It's worth it to convert all your data over to a datetime field

 

ALTER TABLE `records` ADD `new_date` DATETIME NULL ;

 

<?php

mysql_connect( 'localhost', 'root', '' );
mysql_select_db( 'test' );

$q = "SELECT `member`, `date` FROM `records`";
$r = mysql_query( $q );
$values = array();
while ( $d = mysql_fetch_assoc($r) ) {
$a = $d['date'];
$newDate = substr($a,0,4).'-'.substr($a,4,2).'-'.substr($a,6,2).' '.substr($a,8,2).':'.substr($a,10,2).':'.substr($a,12,2);
$values[] = "`new_date` = '$newDate' WHERE `member` = '{$d['member']}'";
}

foreach( $values as $value ) {
$q = "UPDATE `records` SET $value";
mysql_query( $q );
}

?>

 

 ALTER TABLE `records` DROP `date`  

 ALTER TABLE `records` CHANGE `new_date` `date` DATETIME NOT NULL 

Link to comment
Share on other sites

It's worth it to convert all your data over to a datetime field

 

ALTER TABLE `records` ADD `new_date` DATETIME NULL ;

 

<?php

mysql_connect( 'localhost', 'root', '' );
mysql_select_db( 'test' );

$q = "SELECT `member`, `date` FROM `records`";
$r = mysql_query( $q );
$values = array();
while ( $d = mysql_fetch_assoc($r) ) {
$a = $d['date'];
$newDate = substr($a,0,4).'-'.substr($a,4,2).'-'.substr($a,6,2).' '.substr($a,8,2).':'.substr($a,10,2).':'.substr($a,12,2);
$values[] = "`new_date` = '$newDate' WHERE `member` = '{$d['member']}'";
}

foreach( $values as $value ) {
$q = "UPDATE `records` SET $value";
mysql_query( $q );
}

?>

 

 ALTER TABLE `records` DROP `date`  

 ALTER TABLE `records` CHANGE `new_date` `date` DATETIME NOT NULL 

 

their current fieldtype is most likely a TIMESTAMP, which can be used equivalently as a DATETIME field; they need not convert it.  you'll notice that timestamp they posted is pretty much a DATETIME entry, minus the dashes, space and colons.

Link to comment
Share on other sites

  • 3 weeks later...

thanks for the support guys,

 

Unfortunatly changing the DB isnt an option as such, too many scripts already in place around that date format :(

 

I tried the first solution  but I think its pulling all results from the last 7 months as opposed to 7 days,

 

any thoughts?

 

Thanks :)

Link to comment
Share on other sites

20060422081640 <- date like this can be dumped directly into mysql DATETIME or TIMESTAMP field.

 

To convert all rows you'll need three or four queries

 

1. ALTER TABLE

add new DATETIME column

 

2. UPDATE

move data from old column to new column

 

3. ALTER TABLE

drop VARCHAR column

 

There will be more problems with modifying all queries to fetch dates in new format.

 

 

Link to comment
Share on other sites

You can do it with an if statement with the time:

 


//set the time variable
$time = time();

//get the data from the database
$query = "SELECT * FROM records WHERE member =\"".$member."\"";

//get the results
$result = mysql_query($query);

//count the rows
$count = mysql_num_rows($result);

//if an entry exists put it into an array
if($count = 1)
{
$row = mysql_fetch_array($result);
}

//if the time is less than 1 week show all the entries
if($time < ($row['time'] + 604800))
{
display the results
}
else
{
dont display the results
}

 

The reason I am using 604800 is 60 seconds in a min, 60 minutes in an hour, 24 hours in a day, 7 days a week. 60*60*24*7 is how I got that. Hope that helps.

Link to comment
Share on other sites

You can do it with an if statement with the time:

 


//set the time variable
$time = time();

//get the data from the database
$query = "SELECT * FROM records WHERE member =\"".$member."\"";

//get the results
$result = mysql_query($query);

//count the rows
$count = mysql_num_rows($result);

//if an entry exists put it into an array
if($count = 1)
{
$row = mysql_fetch_array($result);
}

//if the time is less than 1 week show all the entries
if($time < ($row['time'] + 604800))
{
display the results
}
else
{
dont display the results
}

 

The reason I am using 604800 is 60 seconds in a min, 60 minutes in an hour, 24 hours in a day, 7 days a week. 60*60*24*7 is how I got that. Hope that helps.

 

this is, frankly, an atrocious way of accomplishing the task. it will pull all records, a waste of resources when you only need those from the last 7 days. on top of that, the time format isn't in seconds, so using a UNIX timestamp for comparison will yield unanticipated results.

Link to comment
Share on other sites

20060422081640 <- date like this can be dumped directly into mysql DATETIME or TIMESTAMP field.

 

To convert all rows you'll need three or four queries

 

1. ALTER TABLE

add new DATETIME column

 

2. UPDATE

move data from old column to new column

 

3. ALTER TABLE

drop VARCHAR column

 

There will be more problems with modifying all queries to fetch dates in new format.

 

 

 

Thanks Guys the support and guidance is as good as always :)

 

Just to throw a spanner in the work the date example I beleive is unix/linux the actual server is a windows server

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.