Frage

I'm showing latest activity on my forum, and I'm extracting "$mytime" from SQL. And now I can't seem to figure this one out:

    $dates = date(" d-m-y",$mytime));
    if($dates == date('d-m-y')) {
      $day_name = 'This day';
   } else if(); 
      $day_name = 'Another day';
    }
    echo "$day_name";

I can't figure out this one: } else if(); {

Is there any more problems in this code?

War es hilfreich?

Lösung 3

Yes there is a problem with that code. As it should look like this:

$dates = date(" d-m-y",$mytime));
if ($dates == date('d-m-y')) {
    $day_name = 'This day';
} elseif (YOU-CAN-COMPARE IT TO SOMETHING ELSE AGAIN IN HERE)  {
    $day_name = 'Another day';
}
echo "$day_name";

Or if you are not willing to compare it for a second time, use this:

$dates = date(" d-m-y",$mytime));
if ($dates == date('d-m-y')) {
    $day_name = 'This day';
} else {
    $day_name = 'Another day';
}
echo "$day_name";

Andere Tipps

It's a bit messy, but you can try something like this:

$dates = date("d-m-y",$mytime)); // you have an extraneous space here
if($dates == date('d-m-y')) {
   $day_name = 'Today';
} else if($dates === date("d-m-y", strtotime("-1 day")); 
   $day_name = 'Yesterday';
}
echo "$day_name";

strtotime reference: http://php.net/manual/en/function.strtotime.php

You'll figure it out.

In php the syntax is:

} elseif( /* conditions */ ) {

Although if there are no conditions, simply do

} else {

UPDATE: See comments

if( date('Ymd') == date('Ymd', strtotime($mytime)) ){
    $day_name = 'This day';
} else {
    $day_name = 'Another day';
}
echo $day_name;
$day_name = ($dates == date('d-m-y')) ? 'This day' : 'Another day';

ternary operator will suffice, or just use

if(this is true){

} else {

}

else if is for other conditions you wanna put and check eg.

    if(){
         if part is true then run this};
    else if(){
              else if part true then run this};

in your case it is just useless ... so keep it or delete it makes no differece ..

To get yesterdays date use this...

date("Y-m-d", strtotime("-1 day"));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top