Php 以倒计时代码显示今天

Php 以倒计时代码显示今天,php,date,countdown,Php,Date,Countdown,嗨,如果给定日期是今天,我想显示今天,否则显示倒计时 这就是我到目前为止所尝试的…它永远不会显示任何东西…我做错了什么 $d1 = new DateTime(); // now $d2 = new DateTime('2018-01-07'); // set the date +1 to compensate for 1-day $diff = $d2->diff($d1); list($y,$m,$d) = explode('-', $diff->format('%y-

嗨,如果给定日期是今天,我想显示今天,否则显示倒计时

这就是我到目前为止所尝试的…它永远不会显示任何东西…我做错了什么

$d1 = new DateTime();  // now
$d2 = new DateTime('2018-01-07');  // set the date +1 to compensate for 1-day  
$diff = $d2->diff($d1);


list($y,$m,$d) = explode('-', $diff->format('%y-%m-%d'));
if ($d1 < $d2) {
    $months = $y*12 + $m;
    $weeks = floor($d/7);
    $days = $d%7;

     if($diff==0)
 {
    echo 'today';
 }
    else
    {
    printf('Countdown To Event : ');
    if ($months) {printf('%d month%s ', $months, $months>1?'s':'');}
    if ($weeks) {printf('%d week%s ', $weeks, $weeks>1?'s':'');}
    if ($days) {printf('%d day%s ', $days, $days>1?'s':'');}
    }


}
$d1=new DateTime();//现在
$d2=新日期时间('2018-01-07');//设置日期+1以补偿1天
$diff=$d2->diff($d1);
列表($y,$m,$d)=分解('-',$diff->格式(“%y-%m-%d”);
如果($d1<$d2){
$months=$y*12+m;
$weeks=底价($d/7);
$days=$d%7;
如果($diff==0)
{
回声“今天”;
}
其他的
{
printf(“事件倒计时:”);
如果($months){printf('%d个月%s',$months,$months,$months>1?'s':'';}
如果($weeks){printf(“%d周%s”,$weeks,$weeks,$weeks>1?'s':”;}
如果($days){printf('%d天%s',$days,$days,$days>1?'s':'';}
}
}

您不正确地使用了DateInteval对象

$d1 = new DateTime();  // now
$d2 = new DateTime('2018-01-07');  // set the date +1 to compensate for 1-day  

// Object of DateInterval class
$diff = $d2->diff($d1);
// Difference in days
$d = $diff->days;

if (! $d) {
    echo 'today';
}
else  {
    $months = $diff->m; 
    $days = $diff->d;
    printf('Countdown To Event : ');
    if ($months) {printf('%d month%s ', $months, $months>1?'s':'');}
//    if ($weeks) {printf('%d week%s ', $weeks, $weeks>1?'s':'');}
    if ($days) {printf('%d day%s ', $days, $days>1?'s':'');}
}

您错误地使用了DateInteval对象

$d1 = new DateTime();  // now
$d2 = new DateTime('2018-01-07');  // set the date +1 to compensate for 1-day  

// Object of DateInterval class
$diff = $d2->diff($d1);
// Difference in days
$d = $diff->days;

if (! $d) {
    echo 'today';
}
else  {
    $months = $diff->m; 
    $days = $diff->d;
    printf('Countdown To Event : ');
    if ($months) {printf('%d month%s ', $months, $months>1?'s':'');}
//    if ($weeks) {printf('%d week%s ', $weeks, $weeks>1?'s':'');}
    if ($days) {printf('%d day%s ', $days, $days>1?'s':'');}
}

这应该可以做到:

<?php

$today = new DateTime();
$date = (new DateTime())->add(new DateInterval('P1D'));

if ($today->format('Y-m-d') === $date->format('Y-m-d')) {
  echo 'TODAY';
} else {
  $d = $today->diff($date);
  $result = '';

  if ($d->years > 1) {
      $result .= $d->years.' Years | ';
  } else if ($d->years == 1) {
      $result .= '1 Year | ';
  } else {
      $result .= '0 Years | ';
  }

  if ($d->months > 1) {
      $result .= $d->months.' Months | ';
  } else if ($d->months == 1) {
      $result .= '1 Month | ';
  } else {
      $result .= '0 Months | ';
  }

  if ($d->days > 1) {
      $result .= $d->days.' Days';
  } else if ($d->days == 1) {
      $result .= '1 Day';
  } else {
      $result .= '0 Days';
  }

  echo $result;
}

?>


工作演示。

这应该可以做到:

<?php

$today = new DateTime();
$date = (new DateTime())->add(new DateInterval('P1D'));

if ($today->format('Y-m-d') === $date->format('Y-m-d')) {
  echo 'TODAY';
} else {
  $d = $today->diff($date);
  $result = '';

  if ($d->years > 1) {
      $result .= $d->years.' Years | ';
  } else if ($d->years == 1) {
      $result .= '1 Year | ';
  } else {
      $result .= '0 Years | ';
  }

  if ($d->months > 1) {
      $result .= $d->months.' Months | ';
  } else if ($d->months == 1) {
      $result .= '1 Month | ';
  } else {
      $result .= '0 Months | ';
  }

  if ($d->days > 1) {
      $result .= $d->days.' Days';
  } else if ($d->days == 1) {
      $result .= '1 Day';
  } else {
      $result .= '0 Days';
  }

  echo $result;
}

?>


工作演示。

这是一段简短的代码,可能会解决您的问题。请尝试以下操作:-

  <?php
$d1 = date("Y-m-d");
$d2 = '2018-01-06';


if(strtotime($d1) == strtotime($d2) )
{
echo 'today';
}
else
{
printf('Countdown To Event : ');
}?>

这是一段简短的代码,可能会解决您的问题。请尝试以下方法:-

  <?php
$d1 = date("Y-m-d");
$d2 = '2018-01-06';


if(strtotime($d1) == strtotime($d2) )
{
echo 'today';
}
else
{
printf('Countdown To Event : ');
}?>


如果事件发生在明天,现在是21:00,那么在您的算法中是“今天”,如果事件发生在明天,现在是21:00,那么在您的算法中是“今天”