Solved

Need help comparing time intervals.

Posted on 2014-11-07
7
74 Views
Last Modified: 2015-01-27
Experts,

I am attempting to compare two dynamic time intervals.

Based on the result of these comparisons I assign a value accordingly.

I believe that I am on the right track but, I think that my PHP syntax in my comparator IF statements are incorrect because I am unable to successfully evaluate AM, PM, and EVE shifts accordingly.

Can someone look at my IF statements below and let me know if I am doing something wrong...seems that most of my comparisons always evaluate the AM shifts incorrectly.

//HERE ARE SOME EXAMPLE TIME INTERVALS THAT COULD BE COMPARED

//SHOULD EVALUATE TO A 'AM SHIFT'
//Example Start Time: 1899-12-30 08:30:00
//Example End Time: 1899-12-30 12:30:00

//SHOULD EVALUATE TO A 'PM SHIFT'
//Example Start Time: 1899-12-30 12:30:00
//Example End Time: 1899-12-30 15:30:00

//SHOULD EVALUATE TO A 'EVE SHIFT'
//Example Start Time: 1899-12-30 15:30:00
//Example End Time: 1899-12-30 20:30:00

//SHOULD EVALUATE TO A 'AM & PM SHIFT'
//Example Start Time: 1899-12-30 8:30:00
//Example End Time: 1899-12-30 15:30:00

////////////////////////////////////////////

//CLEAN UP JOBSTART AND JOBENDTIMES FOR COMPARRISON
$start_time = str_replace('1899-12-30', '', $employee['JobStartTime']);
$start_time = date("g:ia", strtotime($start_time));
$end_time = str_replace('1899-12-30', '', $employee['JobEndTime']);
$end_time = date("g:ia", strtotime($end_time));

//AN AM SHIFT STARTS BEFORE 10AM
$am_shift_key = '10:00am';
//A PM SHIFT STARTS BEFORE 3PM AND ENDS AFTER 3PM
$pm_shift_key_before = '3:00pm';
$pm_shift_key_after = '3:00pm';
//A EVE SHIFT STARTS AFTER 3PM
$eve_shift_key = '2:59pm';
			
//COMPARE JOBSTART AND END TIMES AND SET SHIFT TYPE TO 1 IF STATEMENT EVALUATES TO TRUE

//AM SHIFT
if((strtotime('TODAY '. $start_time)) < (strtotime('TODAY '. $am_shift_key))){
	$am_shift_type = 'AM SHIFT';
}//END ELSE IF

//PM SHIFT
if((strtotime('TODAY '. $start_time)) < (strtotime('TODAY '. $pm_shift_key_before)) && (strtotime('TODAY '. $end_time)) > (strtotime('TODAY '. $pm_shift_key_after)) ){
	$pm_shift_type = 'PM SHIFT';
}//END ELSE IF

//EVE SHIFT
if((strtotime('TODAY '. $start_time)) >= (strtotime('TODAY '. $eve_shift_key))){
	$eve_shift_type = 'EVE SHIFT';
}//END ELSE IF
//END COMPARE JOBSTART AND END TIMES AND SET SHIFT TYPE TO 1 IF STATEMENT EVALUATES TO TRUE

Open in new window

0
Comment
Question by:evibesmusic
  • 4
  • 2
7 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40429759
'strtotime' is specified to return the seconds since 1970 so I don't think your 1899 dates are going to work.

http://php.net/manual/en/function.strtotime.php
0
 

Author Comment

by:evibesmusic
ID: 40429773
@Dave:

I clean up my $start_time and $end_time variables to strip them so they take the following format "00:00am".

Would strtotime() still not work when that is done?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40429779
Have a read of this.  There are many ways to bollix it up, but once you've got the right ideas about time without dates, it will work right all the time.  Just don't choose a date that happens to be February 29, or the boundary of daylight savings time.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40429793
"00:00am" will cause you problems where ever Daylight Savings Time is observed.   'strtotime' returns a 'timestamp' which is the number of seconds since 1970.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40429797
Looking over the code and comments, it seems that we lack some consolidation of thought about the start and end times of the three shifts.  Please post the following details.

AM shift starts at ____ and ends at ____
PM shift starts at ____ and ends at ____
Eve shift starts at ____ and ends at ____

Thanks, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40429798
Regarding the values from strtotime(), this quote is from the online man page:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

Prior to PHP 5.1.0, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40429821
This example is "sort of" right.  I say only "sort of" because someone working more than a standard shift or a split shift may get anomalous results.  The truly correct answer will depend on the rest of your application and what it intends to do with the information about shift-work.  Please look this over and post back with any questions.
http://iconoun.com/demo/temp_evibesmusic.php

<?php // demo/temp_evibesmusic.php
error_reporting(E_ALL);
echo '<pre>';


// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553320.html


function get_shifts($a, $z)
{
    // AM SHIFT RUNS FROM 8AM TO 2:59PM
    $am = range( (int)800,   (int)1499 );

    // PM SHIFT RUNS FROM 3PM TO 9:59PM
    $pm = range( (int)1500,  (int)2199 );

    // EVE SHIFT RUNS FROM 10PM TO 7:59AM
    $ev1 = range( (int)2200, (int)2399 );
    $ev2 = range( (int)0,    (int)799  );

    // NORMALIZE THE HOURS AND MINUTES
    $hra = (int)date('Hi', strtotime($a));
    $hrz = (int)date('Hi', strtotime($z));

    // DETERMINE WHAT SHIFTS THESE TIMES ARE IN
    $out = array();

    if (in_array($hra, $am))  $out['AM'] = 'AM';
    if (in_array($hrz, $am))  $out['AM'] = 'AM';

    if (in_array($hra, $pm))  $out['PM'] = 'PM';
    if (in_array($hrz, $pm))  $out['PM'] = 'PM';

    if (in_array($hra, $ev1)) $out['EV'] = 'EV';
    if (in_array($hrz, $ev1)) $out['EV'] = 'EV';
    if (in_array($hra, $ev2)) $out['EV'] = 'EV';
    if (in_array($hrz, $ev2)) $out['EV'] = 'EV';

    return array_values($out);
}

// TEST CASES
$tests = array
( array('a' => '1899-12-30 08:30:00', 'z' => '1899-12-30 12:30:00')
, array('a' => '1899-12-30 12:30:00', 'z' => '1899-12-30 15:30:00')
, array('a' => '1899-12-30 15:30:00', 'z' => '1899-12-30 20:30:00')
, array('a' => '1899-12-30 08:30:00', 'z' => '1899-12-30 15:30:00')
, array('a' => '1899-12-30 18:30:00', 'z' => '1899-12-30 23:30:00')
, array('a' => '1899-12-30 23:30:00', 'z' => '1899-12-30 06:30:00')
)
;

foreach ($tests as $test)
{
    echo PHP_EOL;
    print_r($test);
    $x = get_shifts($test['a'], $test['z']);
    echo PHP_EOL;
    print_r($x);
    echo PHP_EOL;
}

Open in new window

HTH, ~Ray
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now