PHP Obtain Time Excluding Two Ways of Saturday and Sunday

  • 2021-07-06 10:32:10
  • OfStack

Today, I share with you a function to get a timestamp after 10 days. The key to the program is that he can not calculate the 6th of the week. If you have other needs. It can be changed to N days. It's not Sunday 6th anyway. Ha ha.


// Method 1 : 
<?php
$now = time(); // Specify date usage  $now = strtotime('2014-01-08') ;
$day = 3600*24;
$total = 12;

$days =array() ;

for ($i=2;$i<$total;$i++)
{
    $timer = $now+$day*$i;
    $num= date("N",$timer)-2; // Week 1 Begin 
    if($num>=-1 and $num<=3)
    {
        if(count($days)>=10) break;
        $days[]=date("Y-m-d",$now+$day*$i);
        $total +=1 ;// $total==12 ?$total+1:$total;

    }else
    {
        $total = $total==12 ?$total+1:$total;
    }
}
$i=1;
foreach($days as $day)
{

    echo "$i===>".$day."\n";
    $i++;
}


// Method 2 : 
function get_days ($date="")
{
    $now = empty($date)?time():strtotime($date);
    $days = array();
    $i = 2;
    while(count($days)<10)
    {
        $timer = $now+3600*24*$i;
        $num= date("N",$timer)-2; // Week 1 Begin 
        if($num>=-1 and $num<=3)
        {
            $days[]=date("Y-m-d",$now+3600*24*$i);
        }
        $i++;
    }

 return $days;
}


Related articles: