1

I currently have this code.

function outputCalendarByDateRange($client, $startDate="2011-06-22", 
                               $endDate='2011-06-26') 

I want $startDate and $endDate to reflect today's date and the date three days from now with it automatically updating. I've tried using

$startDate=date("Y-m-D")
$endDate=strtotime(date("Y-m-d", strtotime($todayDate)) . " +3 days");

and

$date1=date("Y-m-D")
$date2=strtotime(date("Y-m-d", strtotime($todayDate)) . " +3 days");
    function outputCalendarByDateRange($client, $startDate=$date1, 
                               $endDate=$date2)

none of these work. How do I make it work?

Thanks!

3
  • Your first $endDate is highly overdone. A simple $endDate = date('Y-m-D', strtotime('+3 days')) will do. Commented Jun 23, 2011 at 3:34
  • @Marc B: Actually, you'd run into problems right around midnight each day doing it that way - better to base it off of $startDate: $endDate = date('Y-m-d', strtotime($startDate . '+3 days')) to handle date changes during execution of the two statements. It will still be off a day by the time the script gets to the third line of code, but at least the two dates will be off uniformly... Commented Jun 23, 2011 at 3:40
  • @Et: in that case, it'd be best to store the raw time value for today, rather than roundtripping everything through strtotime extra times. It's a magical function, but it's NOT efficient. Commented Jun 23, 2011 at 4:01

5 Answers 5

4

you can't use a statement in a function declaration, but you can set the value to null and check it at runtime:

function foo( $bar = null )
{
  if (is_null($bar))
  {
    $bar = 'baz';
  }
  ...code...
}
Sign up to request clarification or add additional context in comments.

1 Comment

also, empty could be used for the check in some cases where array(), "", null, false, etc should trigger the default.
3

You cannot have expressions in the function declaration. But constants could be a workaround for what you want to do.

define("FUNC_CAL_DATE1", date("Y-m-D"));
define("FUNC_CAL_DATE2", strtotime(date("Y-m-d",strtotime($to...

function outputCalendarByDateRange($client,
          $startDate=FUNC_CAL_DATE1, $endDate=FUNC_CAL_DATE2) {

They are actually expressions too, but are specially handled in this context and work where the =$date1 wouldn't.

1 Comment

I'm curious as to whether there could be issues due to caching.
1

You can't pass variables as default values. See below for a possible solution to what you're trying to achieve:

<?php
    error_reporting(E_ALL);

    $defaultStartDate = date("Y-m-d");
    $defaultEndDate   = date("Y-m-d", strtotime($defaultStartDate . " + 3 days"));

    function outputCalendarByDateRange($client, $startDate="", $endDate="") {
        global $defaultStartDate, $defaultEndDate;

        if ($startDate === "") {
            $startDate = $defaultStartDate;
        }
        if ($endDate === "") {
            $endDate = $defaultEndDate;
        }
        echo "Client: " . $client . "<br />";
        echo "Start Date: " . $startDate . "<br />";
        echo "End Date: " . $endDate . "<br />";
    }

    outputCalendarByDateRange("Test Client");

    echo "<br />";

    outputCalendarByDateRange("Test Client #2", date("Y-m-d", strtotime("2011-06-01")), date("Y-m-d", strtotime("2011-07-01")));

?>

Output:

Client: Test Client
Start Date: 2011-06-23
End Date: 2011-06-26

Client: Test Client #2
Start Date: 2011-06-01
End Date: 2011-07-01

Comments

0

You can't have variable default argument values, you'll have to solve this in code:

function outputCalendarByDateRange($client, $startDate = null, $endDate = null) {
    $startDate = $startDate ? $startDate : date('Y-m-d');
    $endDate = $endDate ? $endDate : date('Y-m-d', strtotime('+3 days'));

    ...
}

Calling this function without the second and third argument will use the current date/current date +3, calling it with arguments you can specify your own values.

Comments

-1

I'm gonna take a guess and say that you're trying to set $date1 equal to the variable in the function. That's not necessary, just list them in order. function outputCalendarByDateRange($client, $date1, $date2)

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.