PHP倒计时 - 我如何使用Else If如果还有不到1天的时间

时间:2011-09-30 19:00:10

标签: php

我需要一些php帮助

这是我的代码

<?php
    // Define your target date here
    $targetYear  = get_theme_option('episode_countdown_year');
    $targetMonth = get_theme_option('episode_countdown_month');
    $targetDay   = get_theme_option('episode_countdown_day');
    $targetHour  = 21;
    $targetMinute= 00;
    $targetSecond= 00;
    date_default_timezone_set ( "America/New_York" );
    // Sets the default time zone, in this case GMT -08

    $dateFormat = ( "Y-m-d H:i:s" );
    // Check the PHP Documentation for date uses.
    $targetDate = mktime (
                $targetHour,
                $targetMinute,
                $targetSecond,
                $targetMonth,
                $targetDay,
                $targetYear
    );
    // Sets up our timer
    $actualDate = time();
    // Gets the actual date
    $secondsDiff = $targetDate - $actualDate;
    // Finds the difference between the target date and the actual date
    // These do some simple arithmatic to get days, hours, and minutes out of seconds.
    $remainingDay    = floor ( $secondsDiff / 60 / 60 / 24);
    $remainingHour    = floor ( ( $secondsDiff - ( $remainingDay
                *60 * 60 * 24) ) / 60 / 60 );
    $remainingMinutes = floor ( ( $secondsDiff - ( $remainingDay
                *60 * 60 * 24) - ( $remainingHour * 60 * 60 ) ) / 60 );
    $remainingSeconds = floor ( ( $secondsDiff - ( $remainingDay
                *60 * 60 * 24) - ( $remainingHour * 60 * 60 ) ) -
                ( $remainingMinutes * 60 ) );

    $targetDateDisplay = date($dateFormat,$targetDate);
    $actualDateDisplay = date($dateFormat,$actualDate);
    // Sets up displays of actual and target
    ?>
       <?php if( $targetDate > $actualDate ) {?>  
        description: '<?php echo $remainingDay; ?> Day(s) / <?php echo $remainingHour; ?> Hour(s) / <?php echo $remainingMinutes; ?> Minute(s) Left..',
        picture: '<?php echo $my_url ?>/cache/image-<?php echo $remainingDay; ?>-d.png'
              <?php } else { ?>
        description: 'Now Showing!',
              picture: '<?php echo $my_url ?>/image-now.php'
       <?php } ?>

我的问题是

如果剩下少于1天的话,如何添加else代码

所以我可以隐藏剩余日期,所以只显示小时和分钟

我尝试了这个但是没有用

<?php } else if( $remainingDay < $actualDate ) {?>

1 个答案:

答案 0 :(得分:0)

<?php之后不需要?>就行,只需保持打开即可。但如果您只想让代码少于一天,请尝试:(抱歉,我编辑了这么多)

<?php if( $targetDate > $actualDate ) {
    if($remainingHour < 24){      //main change here..
      //have < 1 day code here
    } else {  
      echo "description: '$remainingDay Day(s) / $remainingHour Hour(s) / $remainingMinutes Minute(s) Left..'
      picture: '$my_url /cache/image-$remainingDay-d.png'";
  } else { ?>
    description: 'Now Showing!',
    picture: '<?php echo $my_url ?>/image-now.php'
   <?php } ?>