我有以下代碼:函數變量是否可以包含參數?
$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;
正如你可以看到,我試圖用「格式(‘YM-d’)」很多次,不想它在多個地方類型,所以我試圖簡單地把它放在一個變量中並使用它。但是,我收到通知:消息:未定義的屬性:DateTime :: $ format('Y-m-d')
什麼是正確的方式去做這件事?
另一個「我怎樣才能減少我在做我的代碼不可讀的費用鍵入擊鍵次數」問題 – 2013-03-06 14:47:11