PHP timestamp and dates issue

92 views Asked by At

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. 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.

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

There are 1 answers

0
John Stadermann On

$date is not defined. Try to define it, and the problem should be solved.