2014-04-17 4 views
0

Im kind'of coincé sur mon projet ... pouvez-vous m'aider?Echo deux variables basées sur d'autres deux variables

Ce que je suis en train de faire est de faire écho à deux endroits différents, soit la variable $open_text et $open_image OU $close_text et $close_image en fonction du jour et l'heure courante de la première déclaration.

J'ai déjà essayé avec switch -> case mais cela n'a pas fonctionné. Une autre idée était d'utiliser des variables variables mais je ne sais pas trop comment l'adapter à mon projet. Ce que j'est:

<?php 
date_default_timezone_set('Europe/Rome'); // timezone 

$weekday = date(l); // today 

// Set open and closing time for each day of the week 
if ($weekday == "Tuesday" || $weekday == "Wednesday" || $weekday == "Thursday" || $weekday == "Friday" || $weekday == "Saturday") { 
    $morning_open_from = "09:15"; 
    $morning_open_to = "12:30"; 
    $afternoon_open_from = "15:00"; 
    $afternoon_open_to = "19:30"; 
} 
else { 
    $morning_open_from = "00:00"; 
    $morning_open_to = "00:00"; 
    $afternoon_open_from = "00:00"; 
    $afternoon_open_to = "00:00"; 
} 

// check if the current time is before or after opening hours 
if (date("H:i") > $morning_open_from || date("H:i") < $morning_open_to && date("H:i") > $afternoon_open_from || date("H:i") < $afternoon_open_to) { 
    $open_text = "<span style='font-size:14px; color: green;'>We're open, call us!</span>"; 
    $open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>0123.456789</span>"; 
} 

// show the checkout button 
else { 
    $close_text = "<span style='font-size:14px; color: red;'>We're closed, contact us!</span>"; 
    $close_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>[email protected]</span>"; 
} 
?> 

Merci beaucoup.

+1

S'il vous plaît montrer où vous voulez faire écho à ces variables – Steve

+0

Peut-être que je ne suis pas assez clair. Je veux appeler et renvoyer des variables dans deux divs différents à l'intérieur de html par paires: soit les variables $ open_text et $ open_image OR $ close_text et $ close_image, en fonction du jour et de l'heure actuels de la première instruction. –

+0

Ok alors dans ce cas la réponse de Jim est ce que vous voulez - n'utilisez pas de noms de variables différents, utilisez $ text et $ image, et changez le contenu avec votre if/else check – Steve

Répondre

1

Où que vous voulez faire l'écho, vous pouvez vérifier si les variables sont définies avant de les afficher:

if(!empty($open_image)){ 
    echo $open_image 
} 

Ou, puisque vraisemblablement close_image $ ou open_image $ sera affiché au même endroit que vous pouvez juste utiliser la même variable:

if (date("H:i") > $morning_open_from || date("H:i") < $morning_open_to && date("H:i") > $afternoon_open_from || date("H:i") < $afternoon_open_to) { 
    $open_text = "<span style='font-size:14px; color: green;'>We're open, call us!</span>"; 
    $open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>0123.456789</span>"; 
}else { 
    $open_text = "<span style='font-size:14px; color: red;'>We're closed, contact us!</span>"; 
    $open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>[email protected]</span>"; 
} 

Puis plus tard, vous pouvez simplement:

echo $open_text; 
echo $open_image; 
+0

Merci Jim, c'est parfait .. j'étais compliquer ma vie en utilisant l'ouverture et la fermeture variable quatre fois :). –

0

Ici, vous allez

date_default_timezone_set('Europe/Rome'); // timezone 

    $weekday = date(l); // today   
    $openDays = array("Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); 
    $morning_open_from = "00:00"; 
    $morning_open_to = "00:00"; 
    $afternoon_open_from = "00:00"; 
    $afternoon_open_to = "00:00"; 

    // Set open and closing time for each day of the week   
    if (in_array($weekday, $openDays)) { 
     $morning_open_from = "09:15"; 
     $morning_open_to = "12:30"; 
     $afternoon_open_from = "15:00"; 
     $afternoon_open_to = "19:30";    
    } 

    // check if the current time is before or after opening hours 
    if ((date("H:i") > $morning_open_from && date("H:i") < $morning_open_to) 
      || 
      (date("H:i") > $afternoon_open_from && date("H:i") < $afternoon_open_to)) { 
     $open_text = "<span style='font-size:14px; color: green;'>We're open, call us!</span>"; 
     $open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>0123.456789</span>"; 
    } 

    // show the checkout button 
    else { 
     $close_text = "<span style='font-size:14px; color: red;'>We're closed, contact us!</span>"; 
     $close_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>[email protected]</span>"; 
    }