32 hours ago excluding weekends with php

So, I have a script that performs several checks 32, 48, and 72 hours ago. Basically I check my database for records that are not older than x hours.

Now it works like this:

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

Now I want this to exclude the weekend. I know that you can use weekdays inside strtotime to get this effect, however this does not work for several hours.

Within 48 hours, this is easy because I can simply do the following:

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

Within 72 hours, it is also easy, because it is 3 days. However, 32 hours creates a problem because it is ± 1.3 days.

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

+4
source share
3 answers

Use strtotime as you initially:

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

Then carry out the calculation for weekends / weekdays 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('Ymd H:i:s', $time); 
+3
source

I'm not sure if this is the right or best way to do this, 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("Ymd H:i:s", $backthen); } return date("Ymd H:i:s", $actual); } 
+1
source

Why not just remove two days and add 16 hours semi-manually to compensate for this?

 $DateTMP = date('Ymd h:i:s',(strtotime(date(Ymd)." -2 weekdays") + (60 * 60 * 16))); 
-2
source

Source: https://habr.com/ru/post/1381291/


All Articles