0

I have two parameters in my stored procedure i.e. @startdate and @enddate. The user selects startdate from text box and enddate also from text box. Then click submit.

I want to make sure if user leaves blank both the boxes, it should display data for the whole current month i.e. 01/08/2012 to 31/08/2012 now.

If user just selects date in any one of the boxes, the other text box should have the same value.

So I have declared variable as below and tried the condition but it always display data for whole month irrespective of what date is selected. I think there is a problem with my if condition.

$startdate = isset($_REQUEST['startdate']) ? $_REQUEST['startdate'] : null;
$enddate   = isset($_REQUEST['enddate']) ? $_REQUEST['enddate'] : null;
if (empty($startdate)) {
    $startdate = $enddate;
}
if (empty($enddate)) {
    $enddate   = $startdate;
}
if (empty($startdate))
    ;
if (empty($enddate))
    ;
$startdate = '01/08/2012';
$enddate   = '31/08/2012';
3
  • 1
    if(empty($startdate)); doesn't do anything. Commented Aug 22, 2012 at 20:22
  • Can you handle this in the stored procedure? Commented Aug 22, 2012 at 20:23
  • 1
    No matter what happens, $startdate = '01/08/2012'; $enddate = '31/08/2012'; resets $startdate and $enddate. Commented Aug 22, 2012 at 20:24

3 Answers 3

4

The last two if statements have no influence of the program flow. You always set $startdate and $enddate to the specified dates.

You need:

if(empty($startdate)) $startdate = '01/08/2012';
if(empty($enddate)) $enddate = '31/08/2012';

If you want to get the first and last day of the actual month every time you could use something like this:

if(empty($startdate)) $startdate = date('01/m/Y', strtotime("-1 month"));
if(empty($enddate)) $enddate = date('d/m/Y', strtotime("-1 month"));
Sign up to request clarification or add additional context in comments.

4 Comments

That's if OP wants $startdate to default to '01/01/2012' and $enddate to default to '31/08/2012'.
Thanks to every one...It worked after I changed my code as per your answers...exactly i expect every month it should automatically change startdate and enddate for e.g. 01/09/2012 and 31/09/2012 for september...is it possible?.
I edited the answer to get you the first/last day of the actual month as well.
@fdomig Thank you but I think there is a minor mistake in your code....it displays from Aug 10 to August 31...not from Aug 01..FYI..my date column is varchar(50) in my table.
1

I think this will work

$startdate = isset($_REQUEST['startdate']) ? $_REQUEST['startdate'] : "";
$enddate = isset($_REQUEST['enddate']) ? $_REQUEST['enddate'] : "";

if (empty($startdate) && (empty($enddate)) {
    $startdate = '01/08/2012';
    $enddate = '31/08/2012';
} elseif (empty($startdate) {
    $startdate = $enddate;
} elseif (empty($enddate) {
    $enddate = $startdate;
}

If both are empty, it uses the start and end of this month; if one or the other is empty, it uses the value for the other.

Comments

1

The logic was not correct. This should work.

$startdate = isset($_REQUEST['startdate']) ? $_REQUEST['startdate'] : null;
$enddate = isset($_REQUEST['enddate']) ? $_REQUEST['enddate'] : null;

if (empty($startdate)) {
$startdate  = $enddate ;
}
elseif (empty($enddate)) {
$enddate = $startdate;
}
elseif(empty($startdate) && empty($enddate)) {
$startdate = '01/08/2012';
$enddate = '31/08/2012';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.