时间脚本

时间:2009-12-03 09:36:24

标签: php datetime mktime

我有一个小脚本,取决于时间和日期;我希望它输出不同的东西:

<?php
    $currenttime = ((date("H")+7). ":" .date("i"));
    if ($currenttime >=  "16:30") {
    $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+2, date("Y"));
    $jsdate = (date("Y"). "-" .date("m"). "-" .(date("d")+1). "-16-30");
    }
    else{
    $tomorrow  = mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"));
    $jsdate = (date("Y"). "-" .date("m"). "-" .date("d"). "-16-30");
    }
    echo "  Time left for delivery on <b><center>" .date("l \\t\h\e jS F", $tomorrow). "</b>:</center>";
    ?>
<center>
<script>
// deadline(date, width, height, style, background, number color, small text color);
deadline("<? echo $jsdate; ?>", "220", "37", "digital2", "#FFFFFF", "#000099", "#000000");
</script>
</center>

例如,在16:30之前的星期一,它需要以下内容:

  • 周二{month} {/ li>的{date}交付时间
  • 然后以DD HH MM SS格式显示16:30之前的剩余时间(最好是服务时间而不是用户当地时间。)

然后在星期一16:30之后需要阅读:

  • 周三{month} {/ li>的{date}交付时间
  • 然后显示剩下的时间 16:30在DD HH MM SS 格式(最好是servertime 而不是用户当地时间。)

然后从周四16:30到周一16:30,需要阅读:

  • 周二{month} {/ li>的{date}交付时间
  • 然后显示剩下的时间 16:30在DD HH MM SS 格式(最好是servertime 而不是用户当地时间。)

我希望这一切都有意义,谢谢你们。

钽, 乙

2 个答案:

答案 0 :(得分:1)

您可以使用strtotime和(php 5.3+)类DateTime来创建,比较和格式化时间戳和间隔。

e.g。 (既没有真正测试也没有完全你所描述的格式):

<?php
$now = new DateTime();
$deliveries = array(
  'monday' => strtotime('monday 16:30'),
  'tuesday' => strtotime('tuesday 16:30')
);

// timestamps in the past are "advanced" one week
foreach($deliveries as &$dt) {
  if ( $dt < $now->getTimestamp() ) {
    $dt = strtotime('+1 week', $dt);
  }
}

// get the diff between 'Now' and the next delivery (smallest timestamp in $deliveries)
$nextDelivery = new DateTime();
$nextDelivery->setTimestamp( min($deliveries) );
$remaining = $nextDelivery->diff($now);

echo $nextDelivery->format(DateTime::RFC2822), " | ", $remaining->format('%d days %H:%i:%S'), "\n";

编辑:不使用DateTime:

<?php
$now = time();
$deliveries = array(
  'monday' => strtotime('monday 16:30', $now),
  'tuesday' => strtotime('tuesday 16:30', $now)
);

// timestamps in the past are "advanced" one week
foreach($deliveries as &$dt) {
  if ( $dt < $now) {
    $dt = strtotime('+1 week', $dt);
  }
}

// get the diff between 'Now' and the next delivery (smallest timestamp in $deliveries)
$nextDelivery = min($deliveries);
// (when) a day has exactly 86400 seconds, an hour 3600 seconds, ...
$remaining = array(
  'days'=> intval(($nextDelivery - $now) / 86400),
  'hours'=> intval( ($nextDelivery - $now) / 3600) % 24,
  'minutes'=> intval( ($nextDelivery - $now) / 60) % 60,
  'seconds'=> ($nextDelivery - $now) % 60
);

echo 'next: ', date(DateTime::RFC2822, $nextDelivery), "\n";
printf('remaining: %d days %02d:%02d:%02d',
  $remaining['days'], $remaining['hours'], $remaining['minutes'], $remaining['seconds']);

答案 1 :(得分:0)

没有修复你的程序,但这一行

$jsdate = (date("Y"). "-" .date("m"). "-" .date("d"). "-16-30");

可以很容易

$jsdate = date("Y-m-d-16-30");

它更具可读性。