blue-lotus
asked on
how to get the todays date and the date 60 days before todays date in php and pass it as a query to mysql?
I need to get data for the last 60 days using mysql and php. using those data's i need to create a graph.
i have a form in the main page which pass the date to the calling page and use that to do the query for date(It works). What does not work is, to be able to query for the last 60 days for default(when some one get to the landing page it should do the query for the last 60 days).
Everything was working before until I tried to query against the date. i get the error "The image http://localhost/mantis/analytics_byseverity_status.php?status=All&start_day=&start_month=&start_year=&end_day=&end_month=&end_year= cannot be displayed, because it contains errors."
I got the today's date and date before 60 days working, but when i try to pass it as a parameter in the function, it does not work. I think it has to do with the making Global variable.
Please help me.
I have attached the code snippet
i have a form in the main page which pass the date to the calling page and use that to do the query for date(It works). What does not work is, to be able to query for the last 60 days for default(when some one get to the landing page it should do the query for the last 60 days).
Everything was working before until I tried to query against the date. i get the error "The image http://localhost/mantis/analytics_byseverity_status.php?status=All&start_day=&start_month=&start_year=&end_day=&end_month=&end_year= cannot be displayed, because it contains errors."
I got the today's date and date before 60 days working, but when i try to pass it as a parameter in the function, it does not work. I think it has to do with the making Global variable.
Please help me.
I have attached the code snippet
<?php
$t_status = $_GET['status'];
$start_date = 0;
$end_date = 0;
$start_day = $_GET['start_day'];
$start_month = $_GET['start_month'];
$start_year = $_GET['start_year'];
$end_day = $_GET['end_day'];
$end_month = $_GET['end_month'];
$end_year = $_GET['end_year'];
if($start_day != null){
global $start_date, $end_date, $start_day, $start_month, $start_year, $end_day, $end_month, $end_year;
$start_date = "$start_year-$start_month-$start_day";
$end_date = "$end_year-$end_month-$end_day";
}
else
{
global $start_date, $end_date, $start_day, $start_month, $start_year, $end_day, $end_month, $end_year;
$today = getdate();
$start_day = $today['mday'];
$start_month = $today['mon'];
$start_year = $today['year'];
$unix = $today['0'];
$diff = $unix - (60*60*24*2*30); #difference of 60 days
$date = getDate($diff);
$end_day = $date['mday'];
$end_month = $date['mon'];
$end_year = $date['year'];
$start_date = "$start_year-$start_month-$start_day";
$end_date = "$end_year-$end_month-$end_day";
}
echo "start_date " . $start_date ."<br>";
echo "end_date " . $end_date ."<br>";
$f_width = gpc_get_int( $t_graph_width, 300 );
$f_token = gpc_get_int( $t_token, 0 );
if ( 0 == $f_token ) {
$t_metrics = chart_severity_status( lang_get( 'severity_enum_string' ), 'severity', $t_status, $start_date, $end_date);
} else {
$t_metrics = graph_total_metrics( unserialize( token_get_value( $f_token ) ) );
}
analytics_pie( $t_metrics, lang_get( 'by_severity_mix' ), $f_width, $f_width, $t_status,$start_year,$start_month,$start_day,$end_year,$end_month,$end_day);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Chakotay505,
I just realized what you meant. I got it working now.
Thank a lot man.
I just realized what you meant. I got it working now.
Thank a lot man.
No problem :-)
ASKER
$today holds the current date. Since it is an array, I used start_day, start_month and start_year to extract the current date and put those together as a string in start_date.
$diff has the unix value for the date 60 days before. I used new unix value to get the date which is stored in $end_date.
Sorry I might have not been clear to explain my problem well enough.
I have landing page which contains the form to select the date range. That is why I have used $_GET prior to extracting the current date. When someone first lands the page it should show the data for the last 60 days. So it should only get current date and 60 days prior date only when someone have landed in the page the very first time. When the user select the date it will get the date from the form using $_GET.
Hope I have made it more clear. Please let me know if I am not clear enough.
Thank you