codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

How can I check to see if a request is being made between certain hours on specific days?


By : , Category : php

I would suggest something like this:

// create start and end date time
$start = new DateTime('8:00am');
$end = new DateTime('5:00pm');

// get the current time
$now = new DateTime();

// note that you can use the < > operators 
// to compare date time objects
if($now >= $start && $now < $end) {
    echo 'during business hours';
}
ReLated :
$hour = 3600; //an hour has 3600 seconds
if($timediff < 2 * $hour )
{
//do something
}

Forget about date(), strtotime(), time(), etc. function, use DateTime :

Use example :

$from = '2013-09-06 15:45:32';
$to   = '2013-09-14 21:00:00';
echo some_func_name($from, $to);

Output :

1 day, 22 hours, 14 minutes, 28 seconds

Function :

function some_func_name($from, $to) {
    $workingDays = [1, 2, 3, 4, 5]; # date format = N
    $workingHours = ['from' => ['08', '00'], 'to' => ['17', '00']];

    $start = new DateTime($from);
    $end = new DateTime($to);

    $startP = clone $start;
    $startP->setTime(0, 0, 0);
    $endP = clone $end;
    $endP->setTime(23, 59, 59);
    $interval = new DateInterval('P1D');
    $periods = new DatePeriod($startP, $interval, $endP);

    $sum = [];
    foreach ($periods as $i => $period) {
        if (!in_array($period->format('N'), $workingDays)) continue;

        $startT = clone $period;
        $startT->setTime($workingHours['from'][0], $workingHours['from'][1]);
        if (!$i && $start->diff($startT)->invert) $startT = $start;

        $endT = clone $period;
        $endT->setTime($workingHours['to'][0], $workingHours['to'][1]);
        if (!$end->diff($endT)->invert) $endT = $end;

        #echo $startT->format('Y-m-d H:i') . ' - ' . $endT->format('Y-m-d H:i') . "
"; # debug

        $diff = $startT->diff($endT);
        if ($diff->invert) continue;
        foreach ($diff as $k => $v) {
            if (!isset($sum[$k])) $sum[$k] = 0;
            $sum[$k] += $v;
        }
    }

    if (!$sum) return 'ccc, no time on job?';

    $spec = "P{$sum['y']}Y{$sum['m']}M{$sum['d']}DT{$sum['h']}H{$sum['i']}M{$sum['s']}S";
    $interval = new DateInterval($spec);
    $startS = new DateTime;
    $endS = clone $startS;
    $endS->sub($interval);
    $diff = $endS->diff($startS);

    $labels = [
        'y' => 'year',
        'm' => 'month',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    ];
    $return = [];
    foreach ($labels as $k => $v) {
        if ($diff->$k) {
            $return[] = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        }
    }

    return implode(', ', $return);
}

This function can be shorter/better; but that is your job now ;)

To calculate the number of days:

var output = "72:50:09",
oneDay = "24:00:00",
out = output.split(":").map(Number),
one = oneDay.split(":").map(Number),
callculate = out[0]/one[0]; //No of days

Now the callculate will hold the number of days.

var min = out[1]; //Will hold the minutes
var sec = out[2]; //Will hold the seconds 
var hrs = out[0]%one[0] //Will hold the hours

Year/Months is what you are calculating from your function.

The logic of your programing isn't right, if you divide by 60, you wont get the minutes of an hour, but if you divide the minutes by 60 then you will get the hour in a set of minutes like this:

Minutes/60 = Hours => Minutes = Hours * 60

in your case, because you are dividing 25.5/60 = 0.425, and it's and minutes is an integer, its only saving the integer part in the variable, that means the 0, and not saving the floating point (.425)

after fixing this, your code should look like this:

#include <iostream>
using namespace std;

int main()
{
    double hours = 0;
    int days = 0;
    int finHours = 0;
    int minutes = 0;

    cout << "Please enter a number of hours greater than zero: 
";


    cin >> hours;
    days = (hours /  24);
    cout << "hours equates to: 
" << "
" << "Days: " << days << "d 
";

    finHours = (hours %  24);
    cout << "Hours: " << finHours << "h 
";

    minutes = (hours * 60);
    cout << "Minutes: " << minutes << "m 
";
    return 0;
}
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .