Bift
Bift

Reputation: 23

Time of day script

I have a small script that depending on the time and day; I would like it to output different things:

<?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>

For instance on a Monday before 16:30 it would need the following:

Then after 16:30 on a Monday it would need to read:

Then from 16:30 on Thursday until Monday 16:30 it would need to read:

I hope this all make sense and thanks for looking.

Ta, B.

Upvotes: 2

Views: 3031

Answers (2)

Peter Stuifzand
Peter Stuifzand

Reputation: 5104

No fix for your program, but this line

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

could just as easy be

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

It's a lot more readable.

Upvotes: 0

VolkerK
VolkerK

Reputation: 96159

You can use strtotime and the (php 5.3+) class DateTime to create, compare and format the timestamps and intervals.

e.g. (neither really tested nor exactly in the format you've described):

<?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";

edit: without using 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']);

Upvotes: 1

Related Questions