1
A
回答
7
試試這個壞男孩。
您可以指定是否將週末包括爲工作日等。不考慮假期。
<?php
function addWorkingHours($timestamp, $hoursToAdd, $skipWeekends = false)
{
// Set constants
$dayStart = 8;
$dayEnd = 16;
// For every hour to add
for($i = 0; $i < $hoursToAdd; $i++)
{
// Add the hour
$timestamp += 3600;
// If the time is between 1800 and 0800
if ((date('G', $timestamp) >= $dayEnd && date('i', $timestamp) >= 0 && date('s', $timestamp) > 0) || (date('G', $timestamp) < $dayStart))
{
// If on an evening
if (date('G', $timestamp) >= $dayEnd)
{
// Skip to following morning at 08XX
$timestamp += 3600 * ((24 - date('G', $timestamp)) + $dayStart);
}
// If on a morning
else
{
// Skip forward to 08XX
$timestamp += 3600 * ($dayStart - date('G', $timestamp));
}
}
// If the time is on a weekend
if ($skipWeekends && (date('N', $timestamp) == 6 || date('N', $timestamp) == 7))
{
// Skip to Monday
$timestamp += 3600 * (24 * (8 - date('N', $timestamp)));
}
}
// Return
return $timestamp;
}
// Usage
$timestamp = time();
$timestamp = addWorkingHours($timestamp, 6);
+0
達姆好孩子;)這真的是工作的人,謝謝! – spamec 2010-01-28 10:33:52
-2
0
一個更緊湊的版本:
function addWhours($timestamp, $hours, $skipwe=false, $startDay='8', $endDay='18')
{
$notWorkingInterval = 3600 * (24 - ($endDay - $startDay));
$timestamp += 3600*$hours;
$our = date('H', $timestamp);
while ($our < $startDay && $our >= $endDay) {
$timestamp += $notWorkingInterval;
$our = date('H', $timestamp);
}
$day = date('N', $timestamp);
if ($skipwe && $day >5) {
$timestamp += (8-$day)*3600*24;
}
return $timestamp;
}
相關問題
- 1. PHP:將時間添加到時間戳
- 2. 將時間添加到時間戳
- 3. 將時間添加到時間戳
- 4. Extjs-將時間戳添加到時間戳
- 5. 將值添加到時間戳
- 6. 將3年添加到時間戳
- 7. 如何將時間戳添加到OutputDebugString()?
- 8. 將時間戳添加到mp4容器
- 9. 將秒添加到時間戳
- 10. 將日期和時間添加到MySQL中的時間戳中
- 11. 爲時間戳添加時間
- 12. 時間戳不工作時間
- 13. 如何使用Ecto的時間戳將時間戳添加到現有表?
- 14. 如何動態添加時間間隔到時間戳?
- 15. Convert.ToDatetime不添加時間戳
- 16. 添加1周時間戳
- 17. 在將數據加載到Hive表時添加時間戳
- 18. JavaPlot時間戳不工作
- 19. 時間戳不工作
- 20. bookshelf.js時間戳不工作
- 21. Android:爲WebView添加時間戳到URL
- 22. 添加字符到時間戳
- 23. Mysql添加1天到時間戳
- 24. 瞬間 - 添加7天的時間戳
- 25. 加快時間戳操作
- 26. UTC時間戳到正常時間戳?
- 27. Django模板邏輯添加時間到時間戳變量?
- 28. 添加時間偏移量從GMT到Unix時間戳PHP
- 29. 將48小時添加到CSV文件中的時間戳
- 30. sqlSave:將數據幀時間戳映射到SQL Server時間戳
我認爲我們需要一些詳細信息。根據您的示例,您是否只處理離散小時,或者它是否實際上使用時間戳,正如您的問題所暗示的那樣?我也懷疑在這個算法的任何實際應用中,你都需要考慮邊界條件,例如下午6點實際上是一個有效的迴應? – 2010-01-28 09:53:11
你是對的...我有開始日期作爲時間戳。然後,我不得不增加幾個小時(像3等)。你關於邊界的問題是正確的(關於這種情況並沒有),而下午6點是無效的,所以結果應該是早上8點(具體的日期時間不只是一小時)。 – spamec 2010-01-28 10:04:03