Ifelse unix timestamp



好的,这是我的问题,我试图在我的问题上使用三元运算符和开关break,但仍然得到了相同的结果。

现在我正在尝试一个ifelse,看看它是否有效,但我仍然得到了同样的错误,它应该回显"是P4",但它却声明这是P1

<?php
//************************************
//https://stackoverflow.com/questions/53180920/unix-time-stamp-adding-  2-weeks-4-weeks-6-weeks-8-weeks/53181428#53181428
//unix time stamp 
//************************************
//1536079080 = 04-09-2018 (Day of the task)
//1541376000 = 11-06-2018 (todays date )
//1542758400 = 11-21-2018 (2 weeks and 1)
//************************************
// > Greater than
// < Less than 
// >=   Greater than or equal to
// <=   Less than or equal to
//************************************
$mod = 1536079080; //unix time stamp 
$moDate = new DateTime('@'. $mod, new   DateTimeZone('America/Los_Angeles')); //Unix change to PST time 
$today = date("d/m/Y"); //todays date

$twoWeeks = time() + (3600*24*14);  //NOW + ( 3600 sec = 1 hour * 24       hours/day * 14 days)
$fourWeeks= time() + (3600*24*28);
$sixWeeks = time() + (3600*24*42);
$eightWeeks = time() + (3600*24*56);

// Less than or equal to and less than
$p1 = ($mod <= $twoWeeks && $mod < $fourWeeks)? true : false;
$res1 = ($p1) ? 'Is a P1' : 'Is not a P1'."<br>";
//Greater than or equal to and less then  6 weeks
$p2 = ($mod >= $fourWeeks && $mod < $sixWeeks)? true : false;
$res2 = ($p2) ? 'is a P2' : 'Is not a P2'."<br>";
//Greater than or equal to and less then 8 weeks
$p3 = ($mod >= $sixWeeks && $mod < $eightWeeks)? true : false;
$res3 = ($p3) ? 'Is a P3' : 'Is not a P3'."<br>";
// is equal to 8 weeks and greater then 8 weeks
$p4 = ($mod == $eightWeeks && $mode > $eightWeeks)? true : false;
$res4 = ($p4) ? 'Is a P4' : 'Is not a P4'."<br>";


echo $res1 , $res2, $res3, $res4;
//Output
//Is a P1
//Is not a P2
//Is not a P3
//Is not a P4
?>

我有点想理解并做你的家庭作业;-(但我认为这里有一个可行的解决方案。。。

$mod = 1546079080; //unix time stamp
$moDate = new DateTime('@'. $mod, new   DateTimeZone('America/Los_Angeles')); 
//Unix change to PST time
$today = date("d/m/Y"); //todays date
echo $mod . "<br>";
$twoWeeks = time() + (3600*24*14);  //NOW + ( 3600 sec = 1 hour * 24       hours/day * 14 days)
$fourWeeks= time() + (3600*24*28);
$sixWeeks = time() + (3600*24*42);
$eightWeeks = time() + (3600*24*56);

// Period 1 between 2 and 4 weeks from now
$p1 = ($mod == $twoWeeks && $mod < $fourWeeks)? true : false;
$res1 = ($p1 ? 'Is a P1' : 'Is not a P1')."<br>";
// Period 2 between 4 and 6 weeks from now
$p2 = ($mod >= $fourWeeks && $mod < $sixWeeks)? true : false;
$res2 = ($p2 ? 'is a P2' : 'Is not a P2')."<br>";
// Period 3 between 6 and 8 weeks from now
$p3 = ($mod >= $sixWeeks && $mod < $eightWeeks)? true : false;
$res3 = ($p3 ? 'Is a P3' : 'Is not a P3')."<br>";
// Period 4 more than 8 weeks in future
$p4 = ($mod >= $eightWeeks) ? true : false;
$res4 = ($p4 ? 'Is a P4' : 'Is not a P4')."<br>";
echo $twoWeeks .'<br>'. $fourWeeks .'<br>'.  $sixWeeks .'<br>'. $eightWeeks .'<br><br>' ;
echo $res1 , $res2, $res3, $res4;
// Output
// 1546079080
// 1543432972
// 1544642572
// 1545852172
// 1547061772
// 
// Is not a P1
// Is not a P2
// Is a P3
// Is not a P4
$mod = 1536079080; //unix time stamp
$moDate = new DateTime('@'. $mod, new   DateTimeZone('America/Los_Angeles')); 
//Unix change to PST time
$today = date("d/m/Y"); //todays date
echo $mod . "<br>";
$twoWeeks = $mod + (3600*24*14);  //NOW + ( 3600 sec = 1 hour * 24       hours/day  * 14 days)
$fourWeeks= $mod + (3600*24*28);
$sixWeeks = $mod + (3600*24*42);
$eightWeeks = $mod + (3600*24*56);

// Period 1 between 2 and 4 weeks from now
$p1 = ($mod == $twoWeeks && $mod < $fourWeeks)? true : false;
$res1 = ($p1 ? 'Is a P1' : 'Is not a P1')."<br>";
// Period 2 between 4 and 6 weeks from now
$p2 = ($mod >= $fourWeeks && $mod < $sixWeeks)? true : false;
$res2 = ($p2 ? 'is a P2' : 'Is not a P2')."<br>";
// Period 3 between 6 and 8 weeks from now
$p3 = ($mod >= $sixWeeks && $mod < $eightWeeks)? true : false;
$res3 = ($p3 ? 'Is a P3' : 'Is not a P3')."<br>";
// Period 4 more than 8 weeks in future
$p4 = ($mod >= $eightWeeks) ? true : false;
$res4 = ($p4 ? 'Is a P4' : 'Is not a P4')."<br>";
echo $twoWeeks .'<br>'. $fourWeeks .'<br>'.  $sixWeeks .'<br>'. $eightWeeks  .'<br><br>' ;
echo $res1 , $res2, $res3, $res4;

最新更新