0

I am looking to add two times together that come from a sql query. Below is my code.

$taskID = $row['taskID'];
$startTimeRaw = $row['startTime'];
$endTimeRaw = $row['endTime'];
$everyMinutesRaw = $row['everyMinutes'];
$startTime = $startTimeRaw->format('H:i:s');
$endTime = $endTimeRaw->format('H:i:s');
$everyMinutes = $everyMinutesRaw->format('H:i:s');

#$latestRunTime = $startTime;
$latestRunTimeRaw = $startTime + $everyMinutes;


echo $startTime."<BR>";
echo $everyMinutes."<BR>";
echo $latestRunTime."<BR>";

This code returns the following

06:05:00
00:15:00
6

The third line of the return should be 06:20:00, how can I make this change. I've played with strtotime and ->format() but none of it seems to get the proper answer. Thoughts?

With the data contained in the other answer I have this

$latestRunTime = strtotime($startTime) + strtotime($everyMinutes);

And it outputs

2733243600

If I format that, I get the following

Fatal error: Call to a member function format() on a non-object in 
6

2 Answers 2

1

This seems to be what you're after:

<?php
$period = new DatePeriod(
        new DateTime('06:05:00'),
        DateInterval::createFromDateString('15 minutes'),
        new DateTime('07:00:00'));
foreach($period as $interval){
        echo $interval->format('c').PHP_EOL;
}

Result:

2013-04-22T06:05:00+02:00
2013-04-22T06:20:00+02:00
2013-04-22T06:35:00+02:00
2013-04-22T06:50:00+02:00

You could also use new DateInterval('PT15M'), or more formally from a time:

new DateInterval('PT'.$everyMinutesRaw->format('H\Hi\Ms\S'));

If you're not interested in all the intervals but just want the first one as per your example:

 $startTimeRaw->add(new DateInterval('PT'.$everyMinutesRaw->format('H\Hi\Ms\S')));
 echo $startTimeRaw->format('H:i:s');
Sign up to request clarification or add additional context in comments.

6 Comments

I had considered using dateinterval pretty strongly. The only issue is that with one query from the database, an interger of 90 might come in, signifying every 1.5 hours. And with a different query, it comes in as 01:30:00. So it is sort of a lose lose for me
Hm, if those are the only 2 formats, can you just not detect it using strstr looking for :? The very first thing to do in this scenario is to detect both inputs & convert them to a single usable known quantity.
Probably? I'm incredibly new to php and my curse of datetimes never working nicely seems to follow me to every language I go to
And those are the two formats depending on how I query the db. everyMinutes is currently stored as an integer
Well, having multiple formats will do that :P (By all means, let the user enter them however they like, but storing them in 1 predefined format if that's under your control really saves you from some headaches later on). Any way: $datetime = new DateInterval('PT'.(strstr($string,':')!==false,DateTime::createFromFormat('H:i:s',$string)->format('H\Hi\Ms\S):$string.'M')); would do most of the trick here (untested code..).
|
1

use this function

function sum_the_time($time1, $time2) {
  $times = array($time1, $time2);
  $seconds = 0;
  foreach ($times as $time)
  {
    list($hour,$minute,$second) = explode(':', $time);
    $seconds += $hour*3600;
    $seconds += $minute*60;
    $seconds += $second;
  }
  $hours = floor($seconds/3600);
  $seconds -= $hours*3600;
  $minutes  = floor($seconds/60);
  $seconds -= $minutes*60;
  if($seconds < 9)
  {
  $seconds = "0".$seconds;
  }
  if($minutes < 9)
  {
  $minutes = "0".$minutes;
  }
    if($hours < 9)
  {
  $hours = "0".$hours;
  }
  return "{$hours}:{$minutes}:{$seconds}";
}

Use this function in every where just call the function when you need..Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.