Jump to content

Generating reports based on date/time conditions


gnawz

Recommended Posts

 

Hi developpers,

I need to generate reports in three ways:

 

I have a Date field in my database...

 

today

$query = 'SELECT * FROM 'table' WHERE Date = today';

 

Past seven days

$query = 'SELECT * FROM 'table' WHERE Date - 7';

 

Choose month( drop down preferably)

 

$query = 'SELECT * FROM 'table' WHERE Date = "'.$_POST['monthselected'].'"';

 

Please help on how to do the SQLs based on date

 

 

Link to comment
Share on other sites

I use this on one of my sites to check if the user is activated within 3 days (72Hours) if not they get deleted. ;D

 

<?php 
$sql = "SELECT * FROM tbltime";
$result = mysql_query($sql);
while($row_rstime = mysql_fetch_array($result)){
$activated = $row_rstime['active'];
$when = strtotime($row_rstime['date_joined']); // "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {  
echo 'Whatever'<br>';
}else{
//Get the date and hour, 72 hours ago
$minus = strtotime("-72 hours");
$date = date("Y-m-d H:i:s", $minus);
$query = "Do something here if needed";
$delete = mysql_query($query);
echo 'Whatever'<br>';
}
}
?>

 

Hope this will help you get an idea?

Otherwise search php.net for date and time functions.

 

Cheers

West

Link to comment
Share on other sites

I have a Date field in my database...

 

today

$query = 'SELECT * FROM 'table' WHERE Date = today';

 

Past seven days

$query = 'SELECT * FROM 'table' WHERE Date - 7';

 

Choose month( drop down preferably)

 

$query = 'SELECT * FROM 'table' WHERE Date = "'.$_POST['monthselected'].'"';

 

Please help on how to do the SQLs based on date

 

my dates are stored in mysql with field type DATETIME so they give date and time.

 

 

 

 

Link to comment
Share on other sites

use MySQL to your full advantage here.  you have several functions at your disposal:

 

NOW() gives the current date and time.

 

DATE_FORMAT() adjusts the date column into a certain format - handy for comparing, for example, month name or number.

 

DATE_SUB() and DATE_ADD() allow you to add or subtract a certain amount of time from a given date/time, in a slightly semantic way (simply choose your interval and how much of that interval, ie. 7 DAY).

 

look at this page for the documentation.

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.