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?