我怎样才能在两个日期之间做出改变?

时间:2018-02-03 15:59:00

标签: php date

我有两个约会。我想要以下行为:

  

如果今天属于开始日期和到期日期,那么做一些事情

但现在两种说法都是正确的,我不知道为什么。代码看起来不错,但是2个日期(start_datemostfinishedweeklystart_dateplaytimeweekly)是相同的。

$timestampmostfinishedweekly = "2018-02-09 16:00:00";
$start_datemostfinishedweekly = date($timestampmostfinishedweekly);
$expiresmostfinishedweekly = strtotime('+7 days', strtotime($timestampmostfinishedweekly));
$expiresmostfinishedweekly2 = strtotime('+14 days', strtotime($timestampmostfinishedweekly));

$timestampplaytimeweekly = "2018-02-02 16:00:00";
$start_dateplaytimeweekly = date($timestampplaytimeweekly);
$expireshetiplaytimeweekly = strtotime('+7 days', strtotime($timestampplaytimeweekly));
$expireshetiplaytimeweekly2 = strtotime('+14 days', strtotime($timestampplaytimeweekly));

// CHALLENGES
if(date('Y-m-d H:i') >= date('Y-m-d H:i', $start_datemostfinishedweekly) && date('Y-m-d H:i') <= date('Y-m-d H:i', $expiresmostfinishedweekly)){
    $xml .= '<label posn="0 -0 0.04" sizen="24 2" scale="0.8" text="Weekly Challenge:"/>';
    $xml .= '<label posn="0 -2.5 0.04" sizen="24 2" scale="0.8" textcolor="FF0" text="Reach the MOST Finish!"/>';
}
if(date('Y-m-d H:i') >= date('Y-m-d H:i', $start_dateplaytimeweekly) && date('Y-m-d H:i') <= date('Y-m-d H:i', $expireshetiplaytimeweekly)){
    $xml .= '<label posn="0 -0 0.04" sizen="24 2" scale="0.8" text="Weekly Challenge:"/>';
    $xml .= '<label posn="0 -2.5 0.04" sizen="28 2" scale="0.8" textcolor="FF0" text="Drive the MOST time on the server!"/>';
    $xml .= '<label posn="0 -5.1 0.04" sizen="32 2" textsize="0.8" text="***the time what you spend with driving are counted"/>';
    //$xml .= '<label posn="0 -5.8 0.04" sizen="32 2" textsize="0.8" text="***times are counted by Finish or when leave server"/>';
}

1 个答案:

答案 0 :(得分:0)

运行代码时出现了这些错误:

java -jar jrobin-1.6.1-SNAPSHOT.jar rrdtool dump usedMemory.rrd > test.xml

但是这段代码完美无缺:

[Sat Feb 03 14:21:29.504277 2018] PHP Notice:  A non well formed numeric value encountered on line 14
[Sat Feb 03 14:21:29.504464 2018] PHP Notice:  A non well formed numeric value encountered on line 18

我还要添加以下代码以供参考和调试

<?php
$timestampmostfinishedweekly = "2018-02-09 16:00:00";
$start_datemostfinishedweekly = new DateTime($timestampmostfinishedweekly);
$expiresmostfinishedweekly = new DateTime($timestampmostfinishedweekly);
$expiresmostfinishedweekly2 = new DateTime($timestampmostfinishedweekly);
$expiresmostfinishedweekly->add(new DateInterval('P7D'));
$expiresmostfinishedweekly2->add(new DateInterval('P14D'));

$timestampplaytimeweekly = "2018-02-02 16:00:00";
$start_dateplaytimeweekly = new DateTime($timestampplaytimeweekly);
$expireshetiplaytimeweekly = new DateTime($timestampplaytimeweekly);
$expireshetiplaytimeweekly2 = new DateTime($timestampplaytimeweekly);
$expireshetiplaytimeweekly->add(new DateInterval('P7D'));
$expireshetiplaytimeweekly2->add(new DateInterval('P14D'));

$now = new DateTime('now');

// CHALLENGES
$xml = '';
if($now >= $start_datemostfinishedweekly && $now <= $expiresmostfinishedweekly){
    $xml .= '<label posn="0 -0 0.04" sizen="24 2" scale="0.8" text="Weekly Challenge:"/>';
    $xml .= '<label posn="0 -2.5 0.04" sizen="24 2" scale="0.8" textcolor="FF0" text="Reach the MOST Finish!"/>';
}
if($now >= $start_dateplaytimeweekly && $now <= $expireshetiplaytimeweekly){
    $xml .= '<label posn="0 -0 0.04" sizen="24 2" scale="0.8" text="Weekly Challenge:"/>';
    $xml .= '<label posn="0 -2.5 0.04" sizen="28 2" scale="0.8" textcolor="FF0" text="Drive the MOST time on the server!"/>';
    $xml .= '<label posn="0 -5.1 0.04" sizen="32 2" textsize="0.8" text="***the time what you spend with driving are counted"/>';
    //$xml .= '<label posn="0 -5.8 0.04" sizen="32 2" textsize="0.8" text="***times are counted by Finish or when leave server"/>';
}

echo '<pre>'.$xml.'</pre>';
//echo '<xmp>'.$xml.'</xmp>'; <- this is the one working for me, but <pre> should do fine.
//echo '<code>'.$xml.'</code>';
?>

我得到了以下结果:

<?php
var_dump($now >= $start_datemostfinishedweekly);
var_dump($now <= $expiresmostfinishedweekly);
var_dump($now >= $start_dateplaytimeweekly);
var_dump($now <= $expireshetiplaytimeweekly);
echo 'Now: '.$now->format('Y-m-d H:i:s').'<br>';
echo '$start_datemostfinishedweekly: '.$start_datemostfinishedweekly->format('Y-m-d H:i:s').'<br>';
echo '$expiresmostfinishedweekly: '.$expiresmostfinishedweekly->format('Y-m-d H:i:s').'<br>';
echo '$start_dateplaytimeweekly: '.$start_dateplaytimeweekly->format('Y-m-d H:i:s').'<br>';
echo '$expireshetiplaytimeweekly: '.$expireshetiplaytimeweekly->format('Y-m-d H:i:s').'<br>';
?>