4

So I have a script that does multiple checks for 32, 48 and 72 hours ago. Basically I check my database for entries that are at least x hours old.

Now this works fine like this:

$date = date('Y-m-d H:i:s',strtotime('-32 hours')); 
$q    = "SELECT * FROM `table` WHERE `date` <= '".$date."'";

Now I want this to exclude weekends. I know you can use weekdays within strtotime to get this effect however this doesn't work for hours.

For 48 hours it's easy because I can simply do the following:

echo date('Y-m-d H:i:s',
          strtotime(date("Y-m-d H:i:s").
          " -2 weekdays ".
          date('H:i:s')));

For 72 hours it's also easy because it's 3 days. However 32 hours poses a problem because it's ±1.3 days.

In conclusion, how do I get the datetime of 32 hours ago excluding weekends.

Kokos
  • 9,051
  • 5
  • 27
  • 44

3 Answers3

3

Use strtotime as you had initially:

$time = strtotime('-32 hours');

Then do the weekend/weekday calculation manually.

// If the day is Sunday or Saturday subtract a full day.
while (date('w', $time) % 6 == 0) {
    $time = strtotime('-1 day', $time);
}
$date = date('Y-m-d H:i:s', $time);
nachito
  • 6,975
  • 2
  • 25
  • 44
  • This works flawlessly! Would give problems when going over 7 days because you will can run into multiple weekends but this isn't the case for me. – Kokos Nov 15 '11 at 13:18
  • You can actually replace the `while` loop with an `if` statement if you also modify the contained `strtotime('last weekday '.date('H:i:s', $time), $time)`. Probably doesn't matter too much. – nachito Nov 15 '11 at 13:24
1

I am not sure if this is correct or the best way to do it but something like:

function getDateBackExcludingWeekend( $hours ) {
  $now = time();
  $secondsBack = $hours * 3600;

  $actual = $now - $secondsBack;
  $monday = strtotime("last monday");

  if( $actual < $monday ) {
    $diff = ($secondsBack - ($now - $monday));
    $backthen = ($monday - 172800 /* two days */) - $diff;

    return date("Y-m-d H:i:s", $backthen);
  }

  return date("Y-m-d H:i:s", $actual);
}
mobius
  • 5,104
  • 2
  • 28
  • 41
-2

Why not just remove two days and add 16 hours semi-manually to make up for it?

$DateTMP = date('Y-m-d h:i:s',(strtotime(date(Y-m-d)." -2 weekdays") + (60 * 60 * 16)));
NekaraNef
  • 197
  • 1
  • 1
  • 8
  • 2
    What would happen on Tuesday at 14:00:00? -2 weekdays makes Friday 14:00:00, and then you add on 16 hours to get Saturday 6:00:00 which is not a weekday. – nachito Nov 15 '11 at 13:02
  • Why do you think I used date('Y-m-d') rather than date('Y-m-d H:i:s')? To circumvent that problem. You're free to propose your own solution, but refrain from giving someone a negative rep for no good reason. – NekaraNef Nov 15 '11 at 13:45
  • Using date('Y-m-d') doesn't circumvent any problem, it just introduces new ones. Now the time will always be fixed at 16:00:00. – nachito Nov 15 '11 at 14:05
  • Erm, no, it would be fixed at 00:00:01, or whatever the first moment of a new day is. I get it you disagree. You're free to do that. There's no denying your solution takes the exact time of the day into account as well, which I personally just wouldn't bother with. But seriously,... if everyone gave everyone they disagreed with a negative rep, this website would turn into a huge mess in a few minutes time. – NekaraNef Nov 17 '11 at 08:37