从php中的函数中排除sat和sun
嗨,我想从这个函数中排除sat和sun。目前这是计算所有天数从php中的函数中排除sat和sun,php,Php,嗨,我想从这个函数中排除sat和sun。目前这是计算所有天数 function human_timing_mins_hrs_days_only($time) { $time = time() - $time; // to get the time since that moment $tokens = array ( 86400 => 'day', 3600 => 'hour', 60 => 'min',
function human_timing_mins_hrs_days_only($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
86400 => 'day',
3600 => 'hour',
60 => 'min',
0 => 'min'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
function human_times_mins_hrs_days_only($time)
{
$time=time()-$time;//获取从该时刻开始的时间
$tokens=数组(
86400=>“天”,
3600=>“小时”,
60=>最小值,
0=>“最小”
);
foreach($unit=>$text形式的令牌){
如果($时间<$单位)继续;
$numberOfUnits=楼层($time/$unit);
返回$numberOfUnits.'.$text.($numberOfUnits>1)?'s':'';
}
}
您可以使用datetime对象进行尝试:
function human_timing_mins_hrs_days_only($time)
{
$now = new DateTime();
$dateInThePast = (new DateTime)->setTimestamp($time);
$interval = $now->diff($dateInThePast, true);
$sundays = intval($interval->days / 7) + ($now->format('N') + $interval->days % 7 >= 7);
$saturdays = intval($interval->days / 7) + ($now->format('N') + $interval->days % 6 >= 6);
$tokens = array (
'days' => 'day',
'h' => 'hour',
'i' => 'min',
);
$units = 0;
$text = 'mins';
foreach ($tokens as $property => $name) {
if ($interval->$property === 0) {
continue;
}
$units = $property === 'days' ? $interval->$property - ($sundays + $saturdays) : $interval->$property;
$text = $name . ($units > 1 ? 's' : '');
}
return $units . ' ' . $text;
}
请注意,此代码未经测试,可能需要进行一些更新才能正常工作。为什么使用MySQL标记?请检查此项。@草莓抱歉,错误地添加了MySQL标记