php 工作时间,php – 计算两个日期之间的工作时间

下面的函数计算两个日期之间的工作时间,以“2013-11-27 13:40”等文本格式提供,工作日时间从9到17(可以更改).

function get_working_hours($from,$to)

{

// timestamps

$from_timestamp = strtotime($from);

$to_timestamp = strtotime($to);

// work day seconds

$workday_start_hour = 9;

$workday_end_hour = 17;

$workday_seconds = ($workday_end_hour - $workday_start_hour)*3600;

// work days beetwen dates, minus 1 day

$from_date = date('Y-m-d',$from_timestamp);

$to_date = date('Y-m-d',$to_timestamp);

$workdays_number = count(get_workdays($from_date,$to_date))-1;

$workdays_number = $workdays_number<0 ? 0 : $workdays_number;

// start and end time

$start_time_in_seconds = date("H",$from_timestamp)*3600+date("i",$from_timestamp)*60;

$end_time_in_seconds = date("H",$to_timestamp)*3600+date("i",$to_timestamp)*60;

// final calculations

$working_hours = ($workdays_number * $workday_seconds + $end_time_in_seconds - $start_time_in_seconds) / 86400 * 24;

return $working_hours;

}

还有两个附加功能.一个返回工作日数组……

function get_workdays($from,$to)

{

// arrays

$days_array = array();

$skipdays = array("Saturday", "Sunday");

$skipdates = get_holidays();

// other variables

$i = 0;

$current = $from;

if($current == $to) // same dates

{

$timestamp = strtotime($from);

if (!in_array(date("l", $timestamp), $skipdays)&&!in_array(date("Y-m-d", $timestamp), $skipdates)) {

$days_array[] = date("Y-m-d",$timestamp);

}

}

elseif($current < $to) // different dates

{

while ($current < $to) {

$timestamp = strtotime($from." +".$i." day");

if (!in_array(date("l", $timestamp), $skipdays)&&!in_array(date("Y-m-d", $timestamp), $skipdates)) {

$days_array[] = date("Y-m-d",$timestamp);

}

$current = date("Y-m-d",$timestamp);

$i++;

}

}

return $days_array;

}

和第二个 – 返回假日数组

function get_holidays()

{

// arrays

$days_array = array();

// You have to put there your source of holidays and make them as array...

// For example, database in Codeigniter:

// $days_array = $this->my_model->get_holidays_array();

return $days_array;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值