views:

422

answers:

2
+2  Q: 

Time of day script

Hi all,

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:

  • Time left for delivery on Tuesday the {date} of {month}
  • Then display how long left until 16:30 in DD HH MM SS format(Preferable to be servertime rather than users local time.)

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

  • Time left for delivery on Wednesday the {date} of {month}
  • Then display how long left until 16:30 in DD HH MM SS format(Preferable to be servertime rather than users local time.)

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

  • Time left for delivery on Tuesday the {date} of {month}
  • Then display how long left until 16:30 in DD HH MM SS format(Preferable to be servertime rather than users local time.)

I hope this all make sense and thanks for looking.

Ta, B.

+1  A: 

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']);
VolkerK
Thanks but I get the following: Call to undefined method DateTime::getTimestamp()
Bift
In that case <?php echo phpversion(); ?> most likely prints something "smaller" than 5.3.0 ? ;-) Since the diff() method is also 5.3+ it doesn't make sense to use DateTime here with older versions of php.
VolkerK
It is 5.2.11 - is there a way to do the same on this version?
Bift
Yes, you can skip DateTime and use strtotime (i.e. unix timestamps) everywhere. Then you have to write the diff() and format() function yourself. If you don't care about leap year, leap seconds and daylight saving time (i.e. a day always has exactly 86400 seconds) it's not that hard.
VolkerK
Thanks for your time, one ting I dont follow is "// timestamps in the past are "advanced" one week"
Bift
I think the `strtotime` monday is the monday of this week, not the next monday.
Peter Stuifzand
Exactly. Without the `next` keyword `monday` means "today, if today is monday or the following monday in all other cases". Therefore, if you call the script on wednesday, $deliveries['monday'] will be a date on monday two days ago and the loop adds one week on all dates. It's not exactly necessary to add a week to _all_ dates, but anyway....
VolkerK
A: 

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.

Peter Stuifzand
thank you mate!
Bift