Having a nightmare at the moment and just can't see why it isn't working
I have a value in the form H:i (ie 10:00, 13:30) etc called $time
What I want to do is create two new values, $startTime which is 30 mins before $time and $endTime which is 30 mins after $time
I have tried the following but just doesn't seem to want to work
$startTime = date("H:i",strtotime('-30 minutes',$time));
$endTime = date("H:i",strtotime('+30 minutes',$time));
If I pass through 10:00 as $time and echo out both $startTime and $endTime I get:
$startTime = 00:30
$startTime = 01:30
$time = strtotime('10:00');
$startTime = date("H:i", strtotime('-30 minutes', $time));
$endTime = date("H:i", strtotime('+30 minutes', $time));
In order for that to work $time has to be a timestamp. You cannot pass in "10:00" or something like $time = date('H:i', '10:00'); which is what you seem to do, because then I get 0:30 and 1:30 as results too.
Try
$time = strtotime('10:00');
As an alternative, consider using DateTime (the below requires PHP 5.3 though):
$dt = DateTime::createFromFormat('H:i', '10:00'); // create today 10 o'clock
$dt->sub(new DateInterval('PT30M')); // substract 30 minutes
echo $dt->format('H:i'); // echo modified time
$dt->add(new DateInterval('PT1H')); // add 1 hour
echo $dt->format('H:i'); // echo modified time
or procedural if you don't like OOP
$dateTime = date_create_from_format('H:i', '10:00');
date_sub($dateTime, date_interval_create_from_date_string('30 minutes'));
echo date_format($dateTime, 'H:i');
date_add($dateTime, date_interval_create_from_date_string('1 hour'));
echo date_format($dateTime, 'H:i');
I usually take a slightly different track to achieve this:
$startTime = date("H:i",time() - 1800);
$endTime = date("H:i",time() + 1800);
Where 1800 seconds = 30 minutes.
Your current solution does not work because $time is a string - it needs to be a Unix timestamp. You can do this instead:
$unix_time = strtotime('January 1 2010 '.$time); // create a unix timestamp
$startTime date( "H:i", strtotime('-30 minutes', $unix_time) );
$endTime date( "H:i", strtotime('+30 minutes', $unix_time) );
$time = 30 * 60; //30 minutes
$start_time = date('Y-m-d h:i:s', time() - $time);
$end_time = date('Y-m-d h:i:s', time() + $time);
echo date( "Y-m-d H:i:s", strtotime("2016-10-10 15:00:00")+(60*30) );//2016-10-10 15:30:00
or
echo date( "H:i:s", strtotime("15:00:00")+(60*30) ); // 15:30:00
or
echo date( "H:i:s", strtotime(date("H:i:s"))+(60*30) ); // 15:30:00
Just to expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval):
// Return adjusted start and end times as an array.
function expandTimeByMinutes( $time, $beforeMinutes, $afterMinutes ) {
$time = DateTime::createFromFormat( 'H:i', $time );
$time->sub( new DateInterval( 'PT' . ( (integer) $beforeMinutes ) . 'M' ) );
$startTime = $time->format( 'H:i' );
$time->add( new DateInterval( 'PT' . ( (integer) $beforeMinutes + (integer) $afterMinutes ) . 'M' ) );
$endTime = $time->format( 'H:i' );
return [
'startTime' => $startTime,
'endTime' => $endTime,
];
}
$adjustedStartEndTime = expandTimeByMinutes( '10:00', 30, 30 );
echo '<h1>Adjusted Start Time: ' . $adjustedStartEndTime['startTime'] . '</h1>' . PHP_EOL . PHP_EOL;
echo '<h1>Adjusted End Time: ' . $adjustedStartEndTime['endTime'] . '</h1>' . PHP_EOL . PHP_EOL;
Related
the format is 02.02.2018 12:00 EST and 02.02.2018 15:00 EST so then I can get 3 hours. I've tried several methods but no luck
$sTimeStr = strtotime('02.02.2018 12:00 EST');
$eTimeStr = strtotime('02.02.2018 15:00 EST');
$cur_time = strtotime( date( "m.d.Y h:i T", time() ));
if( $cur_time > $sTimeStr && $cur_time < $eTimeStr ){
//code to display
}
strtotime returns time in unix timestamp format so you can just subtract the two numbers to get the difference.
$t1 = "02.02.2018 12:00 EST";
$t2 = "02.02.2018 15:00 EST";
$time1 = strtotime($t1);
$time2 = strtotime($t2);
$diff = ($time2 - $time1);
$diff_in_hours = ($diff/60)/60;// $diff_in_hours = 3
$sysdate = date( 'm.d.Y', time() );
$systime = get_date_from_gmt( date( 'H:i', time() ), 'H:i' );
I ended up splitting date and time for better control
have 2 times $dayFrom = 10:00:00; and $dayTo = 12:00:00 i want to divide it in 15 minutes time duration.
Expected Result
10:00:00, 10:15:00, 10:30:00, 10:45:00 and so on
Looking for help
$timeArray = array();
$startTime = new \DateTime("2010-01-01 10:00:00");
$endTime = new \DateTime("2010-01-01 12:00:00");
while($startTime < $endTime) {
$timeArray[] = $startTime->format('H:i:s');
$startTime->add(new \DateInterval('PT 15 M'));
}
echo implode(",",$timeArray);
Here You can go with this code. It will be helpful to you :)
You can also try this -
$dayFrom = strtotime('10:00:00');
$dayTo = strtotime('12:00:00');
while($dayFrom <= $dayTo) {
echo date('H:i:s', $dayFrom);
$dayFrom= strtotime('+ 15 MINUTES', $dayFrom);
}
Output
10:00:00
10:15:00
10:30:00
10:45:00
11:00:00
11:15:00
11:30:00
11:45:00
12:00:00
strtotime()
Another variation on a theme - using DateInterval.
$df='H:i';
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('PT15M');
$ts=date( 'Y-m-d H:i:s', strtotime('10.00am') );
$tf=date( 'Y-m-d H:i:s', strtotime('12.00pm') );
$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );
while( $start->add( $interval ) <= $end ){
echo $start->format( $df ).'<br />';
}
$dayFrom = "10:00:00";
$dayTo = "12:00:00";
while($endTime<$dayTo){
$endTime = strtotime($dayFrom) + 900;
echo date('h:i:s', $endTime);
}
Try this.
how can I find the next closest hour in php
so for example if current time is 4:15 the next hour will be 5, etc
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );
gives me the time from the string and I want to expand on that and get the next closest hour
$nextHour = (intval($date->format('H'))+1) % 24;
echo $nextHour; // 5
Here we go:
<?php
echo date("H:00",strtotime($date. " + 1hour "));
?>
Can you just take pieces (hours, minutes, seconds) and get the next hour?
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );
echo "\n";
$nexthour = ($date->format('H') + ($date->format('i') > 0 || $date->format('s') > 0 ? 1 : 0)) % 24;
echo "$nexthour:00:00";
Supply any eligible date() to:
function roundToNextHour($dateString) {
$date = new DateTime($dateString);
$minutes = $date->format('i');
if ($minutes > 0) {
$date->modify("+1 hour");
$date->modify('-'.$minutes.' minutes');
}
return $date;
}
<?php
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
$date->modify('+1 hour');
echo $date->format('H:i:s').PHP_EOL;
// OR
echo date('H:i:s', strtotime($dateString) + 60 * 60).PHP_EOL;
As I just needed something similar (next full hour) here my solution:
$now = time();
$nextFullHour = date(DATE_ATOM, $now + (3600 - $now % 3600));
By replacing the 3600 e.g. with 60 you get the next full minute...
You can also replace the $now with any other timestamp if you do not need it relative to the current time.
That is my solution:
$dateTime = new \DateTime();
$dateTime->add(new \DateInterval('PT1H'))
->setTime($dateTime->format('H'), '00');
Nobody else used this one so I figured I'd drop it here, simplest one I saw above for an actual timestamp, not just the hour itself.
$now = ''; // empty uses current time, or you can insert a datetime string
$next_hour = date( 'Y-m-d H:00:00', strtotime( $now . ' +1 hour' ) );
try it for current time, if you need put second argument to date function
<?php echo date('H')+1; ?>
very nice stuff
One more:
$current_datetime = new DateTimeImmutable();
$next_full_hour_datetime = $current_datetime
->modify(
sprintf(
'+%d seconds',
3600 - ($current_datetime->getTimestamp() % 3600)
)
);
A little late to this party, but here's a more flexible function to round up a dateTime object by any interval in minutes. You pass in your dateTime object and a rounding interval in minutes, so for an hour you'd just pass in 60, etc.
public function round_up_time( $datetime, $rounding_interval ) {
// get total minutes from the start of the day
$minutes = ( intval( $datetime->format( 'H' ) ) * 60 ) + ( intval( $datetime->format( 'i' ) ) );
// round up the minutes based on the interval we are rounding to
$rounded_minutes = ( intval( $minutes / $rounding_interval ) + 1 ) * $rounding_interval;
// reset our dateTime to the very start of the day
$datetime->setTime( 0, 0 );
// then increase the dateTime by the rounded minutes value
$datetime->modify( '+ ' . $rounded_minutes . ' minutes' );
}
To get the next closest full hour in DateTime:
$date = new DateTime('+1 hour'); //set the next hour
$date->setTime($date->format('H'), '00', '00'); //keep the next hour, set minutes to 00 and seconds to 00
Basically am trying to set a time and a date in PHP then set a time gap which will range between minutes, loop through between a start time and end time echoing something out for each one. Have tried loads of different ways and cant seem to figure a way to set a date and add to it.
This seems the best script I have modified so far:
$minutes = 5;
$endtime = new DateTime('2012-01-01 09:00');
$newendtime = $endtime->format('Y-m-d H:i');
$timedate = new DateTime('2012-01-01 09:00');
while($stamp < $newendtime)
{
$time = new DateTime($timedate);
$time->add(new DateInterval('PT' . $minutes . 'M'));
$timedate = $time->format('Y-m-d H:i');
echo $timedate;
}
$minutes = 5;
$endtime = new DateTime('2012-01-01 09:00');
//modified the start value to get something _before_ the endtime:
$time = new DateTime('2012-01-01 8:00');
$interval = new DateInterval('PT' . $minutes . 'M');
while($time < $endtime){
$time->add($interval);
echo $time->format('Y-m-d H:i');
}
Do everything in seconds, and use php's time(), date(), and mktime functions.
In UNIX Time, dates are stored as the number of seconds since Jan 1, 1970.
You can render UNIX Timestamps with date().
$time = time(); // gets current time
$endtime = mktime(0,0,0, 1, 31, 2012); // set jan 31 # midnight as end time
$interval = 60 * 5; // 300 seconds = 5 minutes
while($time < $endtime){
$time += $interval;
echo date("M jS Y h:i:s a",$time) . "<br>"; // echos time as Jan 17th, 2012 1:04:56 pm
}
date reference:
http://us3.php.net/manual/en/function.date.php (includes superb date format reference too)
mktime reference: http://us2.php.net/mktime
time() only gets the current time, but just for kicks n' giggles: http://us2.php.net/time
And, it's super easy to store in a database!
This function will let you add date to your existing datetime. This will also preserves HH:MM:SS
<?php
function add_date($givendate,$day=0,$mth=0,$yr=0) {
$cd = strtotime($givendate);
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
date('d',$cd)+$day, date('Y',$cd)+$yr));
return $newdate;
}
?>
Usage:
add_date($date,12,0,0);
where $date is your date.
I want to add 5 minutes to this date: 2011-04-8 08:29:49
$date = '2011-04-8 08:29:49';
When I use strtotime I am always getting 1970-01-01 08:33:31
How do I add correctly 5 minutes to 2011-04-8 08:29:49?
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate
Enjoy! :)
Try this:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
$expire_stamp = date('Y-m-d H:i:s', strtotime("+5 min"));
$now_stamp = date("Y-m-d H:i:s");
echo "Right now: " . $now_stamp;
echo "5 minutes from right now: " . $expire_stamp;
Results in:
2012-09-30 09:00:03
2012-09-30 09:05:03
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
For adding
$date = new DateTime('2014-02-20 14:20:00');
$date->add(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It add 5minutes
For subtracting
$date = new DateTime('2014-02-20 14:20:00');
$date->sub(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It subtract 5 minutes
If i'm right in thinking.
If you convert your date to a unix timestamp via strtotime(), then just add 300 (5min * 60 seconds) to that number.
$timestamp = strtotime($date) + (5*60)
Hope this helps
more illustrative for simple and clear solution
$date = '2011-04-8 08:29:49';
$newtimestamp = strtotime($date. ' + 5 minute');//gets timestamp
//convert into whichever format you need
$newdate = date('Y-m-d H:i:s', $newtimestamp);//it prints 2011-04-08 08:34:49