PHP时间戳和日期问题

时间:2015-06-09 14:31:13

标签: php timestamp

所以我在我网站的某个页面上有一段代码,用于处理时间戳。它的作用几乎是从每个单独的采购订单中放置在数据库中的UNIX时间戳。一旦经过一定的时间并且没有对该采购订单进行任何操作,那么指示将开始在页面上闪烁,其中包含过去的小时数。一旦有人采取行动,闪烁的指示就会消失。

现在,一切都运转良好。我遇到的问题是该指标应该只考虑周一至周五。不是周末。此外,我已将时间设置为从上午9点到下午5点,但代码似乎100%跳过所有这些限制,只考虑所有日期和时间。

我已经将代码放在下面,你可以看到我已经设定了日期和时间的限制,但它似乎在某种程度上无效。这个问题将非常感谢任何帮助。

$current_stardate = time();
$past_stardate = $stardate['time_stamp'];
$placer = ($current_stardate - $past_stardate) / 3600;
$from = date("Y-m-d H:i:s", $current_stardate);

$to = date("Y-m-d H:i:s", $past_stardate);

define('DAY_WORK', 28800); // 9 * 60 * 60
define('HOUR_START_DAY', '09:00:00');
define('HOUR_END_DAY', '17:00:00');
$date_begin = $to;
$date_end = $from;

$d1 = new DateTime($date_begin);
$d2 = new DateTime($date_end);

$period_start = new DateTime($d1->format('Y-m-d 00:00:00'));
$period_end   = new DateTime($d2->format('Y-m-d 23:59:59'));
$interval = new DateInterval('P1D');

$period = new DatePeriod($period_start, $interval, $period_end);

$worked_time = 0;
$nb = 0;
foreach($period as $date){
$week_day = $date->format('w'); // 0 (for Sunday) through 6 (for Saturday)
if (!in_array($week_day,array(1, 5)))
{

    if ($date->format('Y-m-d') == $d1->format('Y-m-d'))
    {
        $end_of_day_format = $date->format('Y-m-d '.HOUR_END_DAY);
        $d1_format = $d1->format('Y-m-d H:i:s');
        $end_of_day = new DateTime($end_of_day_format);
        $diff = $end_of_day->diff($d1)->format("%H:%I:%S");
        $diff = split(':', $diff);

        $diff = $diff[0]*3600 + $diff[1]*60 + $diff[0];
        $worked_time += $diff;
    }
    else if ($date->format('Y-m-d') == $d2->format('Y-m-d'))
    {
        $start_of_day = new DateTime($date->format('Y-m-d '.HOUR_START_DAY));
        $d2_format = $d2->format('Y-m-d H:i:s');
        $end_of_day = new DateTime($end_of_day_format);
        $diff = $start_of_day->diff($d2)->format('%H:%I:%S');
        $diff = split(':', $diff);

        $diff = $diff[0]*3600 + $diff[1]*60 + $diff[0];
        $worked_time += $diff;
    }
    else
    {

        $worked_time += DAY_WORK;
    }
}
if ($nb> 10)
die("die ".$nb);
}

$the_work = $worked_time/60/60;

$genesis_stardate = strtotime($stardate['date_purchased']);



if($past_stardate == NULL)
{
    $the_work = NULL;
    $future_days = NULL;
}
else
{
    $future_days = ($current_stardate - $past_stardate) / 3600;
}

1 个答案:

答案 0 :(得分:0)

$date未定义。尝试定义它,问题应该解决。