I've spent so much time on trying to figure out how to create a loop that will echo time between given $start_time and $end_time incrementing it by one hour in format HH:MM. For example if i have $start_time = "22:00" and $end_time="04:00" the output should be like:
22:00
23:00
00:00
01:00
02:00
03:00
04:00
Any idea how to do it?
This is not a very elegant solution but you could do something like this:
<?php
$start_time = "22:00";
$end_time="04:00";
$timestamp = strtotime($start_time);
while(date('H:i', $timestamp) !== date('H:i', strtotime($end_time) + 60*60)){
echo date('H:i', $timestamp) . "\n";
$timestamp += 60*60;
}
With date function with 'H:i' parameter, you can compare only the hour part of the timestamp. That way the for loop will add an hour after every iteration until the variable has passed the end_time (that ensures you echoe the last time too).
Related
Is there a nice simple shorthand way of finding out how many seconds past midnight a certain datetime is? Not how many seconds it is away from now, the seconds past in that day
eg:
2015-04-10 00:00:00 should returns 0
2015-04-12 09:20:00 should returns 33600
2015-04-14 15:20:00 should returns 55200
Is there a nice short method of doing this?
Here is a simple code for you.
Any day code
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime(date("Y-m-d 00:00:00", strtotime($date)));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
Current day code
<?php
$midnight = strtotime("midnight");
$now = date('U');
$diff = $now - $midnight;
echo $diff;
?>
Maybe a more clean code would be to use strtotime("midnight", <EpochTime>);
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
A day is 24 hours is 60 minutes is 60 seconds is 1000 miliseconds, so timestamp % (24*60*60*1000) could work, maybe, at least if you are on the same time zone as the epoch php is using (or whatever you timestamp is coming from). It would work at least for nothing too demanding of accuracy. PHP uses your system's time, so many things will break the idea (change the clock, DST, leap seconds, etc)
I would personally use timestamp - startofday timestamp and calculate using the language's calendar instead.
I'm trying to make strtotime give me a timestamp, but I only know that it's a hour and minute and it's the next in the row. Fx.
If the given hour and minute is 01:00 (AM) I require it to return the timestamp of 01:00 (AM), if it is the day before at 19:00, I would like it to return the timestamp of 01:00 the next day, but if it is after midnight at 00:30, it should return the timestamp 01:00 the same date.
To simplify it, it's the NEXT occurrence of a time.
Strtotime is giving me the time on the same day, if and not the next occurrence.
You can easily generate a timestamp, see if it's already past, and then increment the day.
Something like this perhaps?
$timestamp = strtotime("1:00 am");
if($timestamp < time()) {
$timestamp = strtotime("+1 day",$timestamp);
}
Or if you want to tighten it up to a one-liner:
$string = "1:00 am";
$timestamp = (strtotime($string) > time()) ? strtotime($string) : strtotime("tomorrow " . $string);
I am computing for the time difference of night shift schedule using time only
lets say that I have this data:
$actual_in_time = 6:45 PM //date July 30, 2013
$actual_out_timeout = 7:00 AM //date July 31, 2013
I have to compute for the time difference where the time in should be converted to a whole time, therefore
$actual_in_time = //(some code to convert 6:45 PM to 7:00 PM)
$converted_in_time = $actual_in_time;
Now here is my code to that:
$actual_out_time += 86400;
$getInterval = $actual_out_time - $converted_in_time;
$hours = round($getInterval/60/60, 2, PHP_ROUND_HALF_DOWN);
$hours = floor($hours);
I am not getting the results I wanted. How do you compute for the time difference where the basis is just the time?
Using DateTime object
$start = new DateTime('2000-01-01 6:45 PM');
$end = new DateTime('2000-01-01 7:00 AM');
if ($end<$start)$end->add(new DateInterval('P1D'));
$diff = date_diff($start,$end);
echo $diff->format('%h hours %i minutes');
Add 1 day if your end time is less than start time.
You can use the second parameter in strtotime to get a relative time.
$start = strtotime($startTime);
$end = strtotime($endTime, $start);
echo ($end-$start)/3600;
Hi Suppose I have a timestamp of "2005-10-16 13:05:41".
How would I go about creating a variable that will have a unixtime of the next time it becomes 10am from that initial point?
Would it be something like this?
$timestamp = "2005-10-16 13:05:41";
$tenAMTime = strtotime("next 10am", $timestamp);
I am guessing there is some string I can use to do this? Like "next thursday" example in the PHP documentation.
You nearly had it...
$tomorrowAt10Am = strtotime('+1 day 10:00:00', $timestamp);
Edit:
This was based on the title of your question, for the timestamp of 10am the next day. If you want to output 10am the same day for any times before 10am then you'll want to add some extra logic, as thatidiotguy suggested.
Edit2:
For some reason it won't work if you put all the logic in the same strtotime method, so I made a simple function. You could easily put this into a single line, but I left it as 2 to make it clearer:
$time1 = strtotime('-2 days 09:59:59');
$time2 = strtotime('-2 days 10:00:01');
function next_10am($time)
{
$temp = strtotime('+1 day -10 hours', $time);
return strtotime('10:00', $temp);
}
echo next_10am($time1); // Outputs: 2012-09-08 10:00:00
echo next_10am($time2); // Outputs: 2012-09-09 10:00:00
There is no way for strtotime to know whether or not 10am has already passed, so this is how I would do it:
$timestamp = strtotime("2005-10-16 13:05:41");
// Get current hour and if it is > 10 add a day
if (date('G',$timestamp) >= 10) {
$tenAMTime = strtotime("+1 day 10am", $timestamp);
}
else {
$tenAMTime = strtotime("10am", $timestamp);
}
echo date('r',$tenAMTime); // Comment this out if you want
I'm having problems working through how I would build times that increment by a given multiple. I would like to have a function that would take 3 params, (start, end, offset) and that would give me an output:
The following function would take a start time of 0900, a stop time of 1200 and increment by multiples of 30 minutes.
Would someone please get me started in the right direction? I thought to use to mktime for this but I couldn't get it to work.
myfunction(9, 12, 30)
output:
9:00 am
9:30 am
10:00 am
10:30 am
11:00 am
11:30 am
12:00 am
Function:
function myfunction($start, $end, $step){
$start *= 3600; // 3600 seconds per hour
$end *= 3600;
$step *= 60; // 60 seconds per minute
for($i = $start; $i <= $end; $i += $step)
echo date('h:i a', $i), '<br />';
}
Output:
09:00 am
09:30 am
10:00 am
10:30 am
11:00 am
11:30 am
12:00 pm // You put am here in desired output
// ,but I think you really wanted pm
Codepad
strtotime is another useful function for dealing with dates and times in PHP.
The PHP Manual's function reference is a great place to start when looking for how to do things yourself and taking advantage of built in functions. From that page if you do a search for 'time' you'll find the Date/Time extension which is built in to PHP. You'll see there are many functions available for dealing with date's and time's in PHP.
I would use the time to create a dateTime object. You can format your output using just the time parts, so the day portion is irrelevant. Then you can use standard functions for adding time intervals (some of them are discussed in this question). Just loop over the time addition until the end time is reached or exceeded.
This will also take care of all sorts of special cases that you'd otherwise have to handle on your own, such as AM/PM conversion and start times later than the end time (which will just wrap around to the next day).
<?php
function intervals($start, $end, $interval)
{
$start_date = strtotime($start.':00:00');
$end_date = strtotime($end.'00:00');
$current_date = $start_date;
while($current_date <= $end_date)
{
echo $current_date;
$current_date = strtotime('+ '.intval($interval).' minute', $current_date);
}
}
?>
I guess something like this, is what you looking for.. (untested)
this is my idea
function myfunction($start, $end, $min_increm) {
//just get a datetime do not matter the date
$date = new DateTime('2000-01-01');
//move to start hour, add 9 hour
$start_date = $date->add(new DateInterval(PT{$start}H));
$end date_date = $date->add(new DateInterval(PT{$end}H));
while($date <= $end_date)
//increment minutes and print
echo($date->add(new DateInterval(PT{$min_increm}M))->format("H:m"));
}