是否有可能让函数变量包含参数?

I have the following code:

$posted_on = new DateTime($date_started);
$today = new DateTime('today');
$yesterday = new DateTime('yesterday');
$myFormat = 'format(\'Y-m-d\')';

if($posted_on->{$myFormat} == $today->{$myFormat}) {
    $post_date = 'Today';
}
elseif($posted_on->{$myFormat} == $yesterday->{$myFormat}) {
    $post_date = 'Yesterday';
}
else{
    $post_date = $posted_on->format('F jS, Y');
}

echo 'Started '.$post_date;

As you can see I'm trying to use "format('Y-m-d')" many times, and don't want to type it in multiple places, so I'm trying to simply put it in a variable and use that. However, I get a notice: Message: Undefined property: DateTime::$format('Y-m-d')

What would be the right way to go about doing this?

No, but you can curry the function:

$myFormat = function($obj) {return $obj->format("Y-m-d");};

if( $myFormat($posted_on) == $myFormat($today))

Or more cleanly:

class MyDateTime extends DateTime {
    public function format($fmt="Y-m-d") {
        return parent::format($fmt);
    }
}
$posted_on = new MyDateTime($date_started);
$today = new MyDateTime("today");
$yesterday = new MyDateTime("yesterday");

if( $posted_on->format() == $today->format()) {...
$myFormat = 'Y-m-d';
...
$today->format($myFormat);
...
$posted_on = new DateTime($date_started);
$today = new DateTime('today');
$yesterday = new DateTime('yesterday');
$myFormat = 'Y-m-d';

if($posted_on->format($myFormat) == $today->format($myFormat)) {
    $post_date = 'Today';
}
elseif($posted_on->format($myFormat) == $yesterday->($myFormat)) {
    $post_date = 'Yesterday';
}
else{
    $post_date = $posted_on->format('F jS, Y');
}

echo 'Started '.$post_date;

That is the best you can do. I would put the format somewhere in a constant or a config file but w/e. What you are trying to do is possible but it's so horrible that I actually started crying when I read it.

Also in this case I would do something like this

$interval   = $posted_on->diff(new DateTime('today'));
$postAge    = $interval->format('%d'); // Seems to be the best out of many horrible options
if($postAge == 1)
{
    $post_date = 'Today';
}
else if($postAge == 2)
{
    $post_date = 'Yesterday';
}
else
{
    $post_date = $posted_on->format('F jS, Y');
}