-1

I have datetimes in the following format:
Start: 2017-07-16 20:00
End: 2017-07-16 23:30

Start: 2017-07-18 21:30
End: 2017-07-19 00:30

I need to tell from these intervals how many hours (in 0,5 increments) are spent between 18:00-22:00 and 22:00-06:00 in total for a month.

Thanks in advance for any hint.

The current code I have is this, but I'm not sure if it is covering all timeframe possibilities:

<?php
  date_default_timezone_set("UTC");

  $start = array(
    "2017-07-16 21:30:00",
    "2017-07-16 18:00:00"
  );
  $end = array(
    "2017-07-17 00:30:00",
    "2017-07-16 18:30:00"
  );

  $amount_low = 0;
  $amount_high = 0;

  for($i = 0; $i < sizeof($start); $i++) {
    $start_time = date("H:i:s", strtotime($start[$i]));
    $end_time   = date("H:i:s", strtotime($end[$i]));

    $start_date = date("Ymd", strtotime($start[$i]));
    $end_date   = date("Ymd", strtotime($end[$i]));

    // getting chunk before 22:00 if
    if(
        (strtotime($start[$i]) >= strtotime($start_date . " 18:00") && strtotime($start[$i]) < strtotime($start_date . " 22:00"))
        &&
        $start_date < $end_date
      ) {

      $interval_low = strtotime($start_date . " 22:00") - strtotime($start[$i]);
      $amount_low += ceil($interval_low / 1800) / 2;

    }

    //amount_high
    if(strtotime($start[$i]) > strtotime($start_date . " 22:00") && strtotime($start[$i]) < strtotime($start_date . " 06:00")) {

      $interval_high = strtotime($end[$i]) - strtotime($start[$i]); //needs further things
      $amount_high += ceil($interval_high / 1800) / 2;

    } elseif (strtotime($start[$i]) < strtotime($start_date . " 22:00") && strtotime($end[$i]) > strtotime($start_date . " 22:00")) {

      $interval_high = strtotime($end[$i]) - strtotime($start_date . " 22:00");
      $amount_high += ceil($interval_high / 1800) / 2;
    } else {

      $interval_low = strtotime($end[$i]) - strtotime($start[$i]);
      $amount_low += ceil($interval_low / 1800) / 2;

    }

  }
  echo $amount_low;
  echo "\n$amount_high";
?>
ediets
  • 1
  • 1
  • 2
    Can you supply some code and expected output? Is the answer to the first example 2 h and 1.5 h? – Andreas Jul 18 '17 at 19:51
  • @Andreas yes, for the first example indeed it should give the mentioned amounts. The code which I tried to implement is the same mentioned below by the time difference between two dates. – ediets Jul 18 '17 at 20:01
  • The reason we ask for code is because we want to know what/that you have tried. And to not have to start from scratch. And to use the same variables that you do so that you don't have to change them to your variables. – Andreas Jul 18 '17 at 20:06
  • The code was added to the question, I believe this is not a duplicate. – ediets Jul 21 '17 at 19:05
  • Too late. Take this as your lesson. Write your questions so that there can't be any follow up questions or anything that is unclear. My advice. Delete this question and write a new one. And keep in mind this lesson. – Andreas Jul 21 '17 at 19:10
  • Note taken and not being offensive or anything but based on your answer if something is unclear why one marks it as a duplicate? Too late because judgment comes too early? – ediets Jul 21 '17 at 19:15
  • Judgement comes very fast here on SO. If your question is unclear it will be put on hold. If it's unclear and is similar to another it will be deemed as duplicate and put on hold. Even if you try to correct it later it's still to late. If not everything is perfect within 5-10 minutes after posting a question it's usually put on hold or duplicated. Here everything has to be crystal clear, with code, what you have tried, what the output is of that try and perhaps 'i have tried to find solution by googling but have not found anything'. That means you have multiple tries, code, failed output and – Andreas Jul 21 '17 at 19:25
  • What your expected output should look like. If you don't have all that. The question may be unclear or you have not tried enough. Or people don't know what your "answer" is. Tick all the boxes and I bet you will get upvotes instead of downvotes – Andreas Jul 21 '17 at 19:27
  • @Andreas thank you very much for the clarification, I appreciate it. Also, I would create a new question and delete this, but as there are already two answer I'm not allowed to do it. – ediets Jul 21 '17 at 19:30
  • Ok. I see. The reason I adviced you to delete this is because some will digg up the fact that you have a duplicate question and it will be put on hold for that reason. Sometimes it's good to be up front about you have a duplicate question sometimes it's not. I can't give you an advice on that. Do what you think is the best. – Andreas Jul 21 '17 at 19:40

2 Answers2

0

I believe a part of your solution is found here from Aillyn

You can convert them to timestamps and go from there:

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

This will give you a rounded hour difference between the two times. You could do something similar, just apply it for each interval for which ever month and adjust your math. There's not really going to be a single PHP function you can call to solve this.

mahbad
  • 149
  • 1
  • 2
  • 19
  • 1
    Please don't post link only answers. If the link changes/get's removed, the answer will be useless for future visitors. Include the actual implementation in your answer and post the link as an extra reference. However, if the link is to another post here on SO that solves the issue, the question should be marked as a duplicate instead. – M. Eriksson Jul 18 '17 at 19:55
  • Looks like a duplicate to me. – Don't Panic Jul 18 '17 at 19:59
  • Not sure about that @Dont OP asks for a months total and in two separate "categories" if understand it correctly. – Andreas Jul 18 '17 at 20:01
  • thanks @MagnusEriksson – mahbad Jul 18 '17 at 20:03
  • But like I said, if the other post answers the question, then the question should be marked as a duplicate of that post instead. Don't duplicate existing answers. It's OK if you need to change or keep building on that answer, but not if it's a direct copy. – M. Eriksson Jul 18 '17 at 20:12
  • Even if we discard the fact of it being a duplicate answer and that it's a link only answer it's still not correct. OP asks for 0.5 h rounding. Your code will round normally. So 0.72 = 0.7 instead of 0.5 – Andreas Jul 18 '17 at 20:16
0

Would this work for you?

$start = strtotime('2017-07-16 20:00');
$end = strtotime('2017-07-16 23:30');

$interval = $end - $start;

$hours = floor($interval / 1800)/2;

echo($hours);

This will display the total hours (in 0,5 increments) between the two dates (rounding down, so if it's 55 minutes, it's 0,5 hours; replace 'floor' with 'ceil' for the opposite).

José Colina
  • 21
  • 1
  • 10
  • This is what I got now. This should be modified further for the above mentioned intervals and to check how many hours is in one part and the other. – ediets Jul 18 '17 at 20:05
  • 1
    @ediets - This is a good reason why it's important for you to add your existing code. We don't really want to spend our time writing examples that's the same as the code you already have. – M. Eriksson Jul 18 '17 at 20:10
  • @MagnusEriksson - This is what I was trying to do, but I'm not sure if it would cover all the possible time combinations: [code](https://pastebin.com/SgcY62gm) – ediets Jul 19 '17 at 17:27
  • @ediets - Add _all relevant_ code to your question, instead of link to an external site. – M. Eriksson Jul 19 '17 at 20:49
  • @MagnusEriksson done, please see at original question. – ediets Jul 20 '17 at 19:07