简体   繁体   English

PHP时间戳和日期问题

[英]PHP timestamp and dates issue

So I have a piece of code on a certain page of my site that does things with timestamps. 因此,我在网站的特定页面上有一段代码,该代码可以处理时间戳。 Pretty much what it does is there is a UNIX timestamp that is placed in the database from each individual Purchase Order. 它所做的几乎是每个单独的采购订单将UNIX时间戳记放置在数据库中。 Once a certain amount of time has passed and nothing has been done to that Purchase Order then an indication will begin flashing on the page with the amount of hours that is past due. 一旦经过了一定的时间并且对该采购订单未执行任何操作,则指示将开始在页面上闪烁,显示已过期的小时数。 Once someone takes action then the flashing indication goes away. 一旦有人采取行动,闪烁的指示就会消失。

Now, everything is working perfectly fine. 现在,一切正常。 The issue I am having is that the indicator should only take Monday thru Friday into account. 我遇到的问题是,该指标仅应考虑周一至周五。 Not the weekends. 不是周末。 Also, I've set the hours from 9am to 5pm est but the code seems to 100% skip all these restrictions and just takes all days and times into consideration. 另外,我将时间设置为从上午9点到下午5点,但是代码似乎100%跳过了所有这些限制,只考虑了所有天数和时间。

I've placed the code below and as you can see I've set the restrictions of days and time but it seems to be voided somehow. 我将代码放在下面,您可以看到我设置了日期和时间的限制,但是它似乎以某种方式无效。 Any help would be much appreciated with this issue. 任何帮助将不胜感激与此问题。

$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;
}

$date is not defined. $date未定义。 Try to define it, and the problem should be solved. 尝试定义它,问题应该得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM