mardi 30 janvier 2018

Echo message if between two hours

I am trying to echo a message depending on the time of the page. I need to show the type of service of a coach company. These services are "Semidirecte (sd)", "Pobles (p)" and "Exprés (e)". What I have written is something like: if we are between X hours or X hours, etc; echo X, elseif we are between Y hours or Y hours, etc; echo Y else if we are between Z hours or Z hours; echo Z. This is my actual code:

<?php 
$now = new DateTime();
$v0600 = new DateTime('6:00');
$v0630 = new DateTime('6:30');
$v0650 = new DateTime('6:50');
$v0705 = new DateTime('7:05');
$v0725 = new DateTime('7:25');
$v0755 = new DateTime('7:55');
$v0825 = new DateTime('8:25');
$v0840 = new DateTime('8:40');
$v0900 = new DateTime('9:00');
$v0925 = new DateTime('9:25');
$v1000 = new DateTime('10:00');
$v1020 = new DateTime('10:20');
$v1030 = new DateTime('10:30');
$v1115 = new DateTime('11:15');
$v1200 = new DateTime('12:00');
$v1210 = new DateTime('12:10');
$v1230 = new DateTime('12:30');
$v1300 = new DateTime('13:00');
$v1325 = new DateTime('13:25');
$v1400 = new DateTime('14:00');
$v1435 = new DateTime('14:35');
$v1450 = new DateTime('14:50');
$v1505 = new DateTime('15:05');
$v1525 = new DateTime('15:25');
$v1600 = new DateTime('16:00');
$v1620 = new DateTime('16:20');
$v1635 = new DateTime('16:35');
$v1700 = new DateTime('17:00');
$v1725 = new DateTime('17:25');
$v1805 = new DateTime('18:05');
$v1820 = new DateTime('18:20');
$v1835 = new DateTime('18:35');
$v1910 = new DateTime('19:10');
$v2000 = new DateTime('20:00');
$v2010 = new DateTime('20:10');
$v2035 = new DateTime('20:35');
$v2100 = new DateTime('21:00');
$v2125 = new DateTime('21:25');
$v2155 = new DateTime('21:55');
$v2210 = new DateTime('22:10');
$v2305 = new DateTime('23:05');
$v0500 = new DateTime('05:00');
$sd = Semidirecte;
$p = Pobles;
$e = Exprés;

if ($now > $v0500 && $now < $v0600 or $now > $v0630 && $now < $v0650 or $now > $v0840 && $now < $v0900 or $now > $v0925 && $now < $v1000 or $now > $v1115 && $now < $v1200 or $now > $v1325 && $now < $v1400 or $now > $v1525 && $now < $v1600 or $now > $v1635 && $now < $v1700 or $now > $v1725 && $now < $v1805 or $now > $v1910 && $now < $v2000 or $now > $v2035 && $now < $v2100 or $now > $v2125 && $now < $v2155){
echo $sd; 
} elseif ($now > $v0650 && $now < $v0705 or $now > $v0825 && $now < $v0840 or $now > $v1000 && $now < $v1020 or $now > $v1200 && $now < $v1210 or $now > $v1230 && $now < $v1300 or $now > $v1435 && $now < $v1450 or $now > $v1450 && $now < $v1505 or $now > $v1600 && $now < $v1620 or $now > $v1805 && $now < $v1820 or $now > $v2000 && $now < $v2010 or $now > $v2155 && $now < $v2210){
echo $e;    
} elseif ($now > $v0600 && $now < $v0630 or $now > $v0705 && $now < $v0725 or $now > $v0725 && $now < $v0755 or $now > $v0755 && $now < $v0825 or $now > $v0900 && $now < $v0925 or $now > $v1020 && $now < $v1030 or $now > $v1030 && $now < $v1115 or $now > $v1210 && $now < $v1230 or $now > $v1300 && $now < $v1325 or $now > $v1400 && $now < $v1435 or $now > $v1505 && $now < $v1525 or $now > $v1620 && $now < $v1635 or $now > $v1700 && $now < $v1725 or $now > $v1820 && $now < $v1835 or $now > $v1835 && $now < $v1910 or $now > $v2010 && $now < $v2035 or $now > $v2100 && $now < $v2125 or $now > $v2210 && $now < $v2305){
echo $p;
} else {
echo "No data"; 
}
?>

What am I doing wrong? Is there a simpler script to do what I want? I have a public website running my entire script: http://poldiloli.com/bus/setmanal.php, but it is showing what it wants at the time that it wants.

Aucun commentaire:

Enregistrer un commentaire