我有以下代码:函数变量是否可以包含参数?
$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