Solved

Need help comparing time intervals.

Posted on 2014-11-07
7
80 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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 109

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 109

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 109

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cpanel file manager 8 39
PHP Curl to output a url 7 46
PHP Mail error 3 26
Log in through ID 5 17
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

791 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