?
Solved

Need help comparing time intervals.

Posted on 2014-11-07
7
Medium Priority
?
99 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 84

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 111

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 84

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 111

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

592 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