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.
Related
I've got the tutorial from this post.
The PHP code:
$start = '20:00:00';
$end = '07:59:59'; //need to show until tomorrow time.
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
Now I need to show the time from 20:00:00 until tomorrow at 07:59:59.
Current code when I'm trying to run it got no result (empty).
The problem with your code is that it's just comparing the times not dates so obviously in your example the start time is greater than the end time.
Try the following example it works perfectly.
<?php
$TodayDate = date("Y-m-d");
$start = $TodayDate.' 20:00:00';
$TomorrowDate = new DateTime('tomorrow');
$TomorrowDate = $TomorrowDate->format('Y-m-d');
$end = $TomorrowDate.' 07:59:59'; //need to show until tomorrow time.
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
?>
20:00 - 20:30
20:30 - 21:00
21:00 - 21:30
21:30 - 22:00
22:00 - 22:30
22:30 - 23:00
23:00 - 23:30
23:30 - 00:00
00:00 - 00:30
00:30 - 01:00
01:00 - 01:30
01:30 - 02:00
02:00 - 02:30
02:30 - 03:00
03:00 - 03:30
03:30 - 04:00
04:00 - 04:30
04:30 - 05:00
05:00 - 05:30
05:30 - 06:00
06:00 - 06:30
06:30 - 07:00
07:00 - 07:30
07:30 - 08:00
You could use the DateTime class with it's associated methods - add ~ this would perhaps simplify the task? It is simple to modify the above to work with a pre-defined start time / end time
$now = new DateTime();
$end = new DateTime( date( DATE_ATOM, strtotime( 'now + 1day' ) ) );
while( $now->add( new DateInterval('PT30M') ) < $end ){
echo $now->format( 'H:i:s' ) . '<br />';
}
To use the actual desired start/end times
$start = date( DATE_ATOM, strtotime( 'today 8pm' ) );
$finish = date( DATE_ATOM, strtotime( sprintf( '%s + 1day',$start ) ) );
$now = new DateTime( $start);
$end = new DateTime( $finish );
while( $now->add( new DateInterval('PT30M') ) < $end ){
echo $now->format( 'H:i:s' ) . '<br />';
}
Just add some date.
Search for 08:00:00 of which day?
$start = '01/01/2019 20:00:00';
$end = '01/02/2019 08:00:00';
$time = strtotime($start);
$timeStop = strtotime($end);
while ($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
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
I need to divide time period for example from:
2015-11-22 11:22:33 to 2015-11-24 02:02:04
into something like this:
2015-11-22 11:22:33 - 2015-11-22 23:59:59
2015-11-23 00:00:00 - 2015-11-23 23:59:59
2015-11-24 00:00:00 - 2015-11-24 02:02:04.
It has to work also for periods shorter than 24h, so for
2015-11-22 11:22:33 to 2015-11-23 02:02:04
I need this:
2015-11-22 11:22:33 - 2015-11-22 23:59:59
2015-11-23 00:00:00 - 2015-11-23 02:02:04.
I found almost perfect piece of code, but it only works for periods longer than 24h and I don't know how to tune it.
<?php
$start_date = '27:04:2013';
$start_time = '16:30';
$end_date = '29:04:2013';
$end_time = '22:30';
// Date input strings and generate a suitable DatePeriod
$start = DateTime::createFromFormat("d:m:Y H:i", "$start_date $start_time");
$end = DateTime::createFromFormat("d:m:Y H:i", "$end_date $end_time");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
// Get midnight at start of current day
$date_start = clone $date;
$date_start->modify('midnight');
// Get 23:59:59, end of current day
// (moving to midnight of next day might be good too)
$date_end = clone $date;
$date_end->modify('23:59:59');
// Take care of partial days
$date_start = max($start, $date_start);
$date_end = min($end, $date_end);
// Here you would construct your array of
// DateTime pairs, or DateIntervals, as you want.
printf(
"%s -> %s \n",
$date_start->format('Y-m-d H:i'),
$date_end->format('Y-m-d H:i')
);
}
?>
Try this:
$date1 = '2015-11-22 11:22:33';
$date2 = '2015-11-23 12:22:34';
$f1 = strtotime($date1);
$f2 = strtotime(substr($date1, 0, 10) . " 23:59:59");
while($f2 < strtotime($date2)) {
print(date('Y-m-d H:i:s',$f1) .' - ' .date('Y-m-d H:i:s',$f2).'<br>');
$f1 = strtotime(date('Y-m-d H:i:s', $f2) .' +1 second');
$f2 = strtotime(date('Y-m-d H:i:s', $f2) .' +1 day');
}
print(date('Y-m-d H:i:s',$f1) .' - ' .$date2.'<br>');
See here: http://sandbox.onlinephpfunctions.com/code/77eaae15fdd7c0d2ca1f02a2d225c17199218819
$datetime1 = new DateTime('2015-11-22 11:22:33');
$datetime2 = new DateTime('2015-11-23 02:02:04');
$interval = $datetime1->diff($datetime2);
var_dump($interval->format('%y-%m-%d %h:%i:%s'));
// RESULT: string(14) "0-0-0 14:39:31"
References:
http://php.net/manual/de/datetime.diff.php
http://php.net/manual/de/class.dateinterval.php
http://php.net/manual/de/dateinterval.format.php
How can I get the timestamp of 12 o'clock of today, yesterday and the day before yesterday by using strtotime() function in php?
12 o'clock is a variable and would be changed by user.
$hour = 12;
$today = strtotime($hour . ':00:00');
$yesterday = strtotime('-1 day', $today);
$dayBeforeYesterday = strtotime('-1 day', $yesterday);
strtotime supports a number of interesting modifiers that can be used:
$hour = 12;
$today = strtotime("today $hour:00");
$yesterday = strtotime("yesterday $hour:00");
$dayBeforeYesterday = strtotime("yesterday -1 day $hour:00");
echo date("Y-m-d H:i:s\n", $today);
echo date("Y-m-d H:i:s\n", $yesterday);
echo date("Y-m-d H:i:s\n", $dayBeforeYesterday);
It works as predicted:
2011-01-24 12:00:00
2011-01-23 12:00:00
2011-01-22 12:00:00
OO Equivalent
$iHour = 12;
$oToday = new DateTime();
$oToday->setTime($iHour, 0);
$oYesterday = clone $oToday;
$oYesterday->modify('-1 day');
$oDayBefore = clone $oYesterday;
$oDayBefore->modify('-1 day');
$iToday = $oToday->getTimestamp();
$iYesterday = $oYesterday->getTimestamp();
$iDayBefore = $oDayBefore->getTimestamp();
echo "Today: $iToday\n";
echo "Yesterday: $iYesterday\n";
echo "Day Before: $iDayBefore\n";
You can easily find out any date using DateTime object, It is so flexible
$yesterday = new DateTime('yesterday');
echo $yesterday->format('Y-m-d');
$firstModayOfApril = new DateTime('first monday of april');
echo $firstModayOfApril->format('Y-m-d');
$nextMonday = new DateTime('next monday');
echo $nextMonday->format('Y-m-d');
to get start of day yesterday
$oDate = new DateTime();
$oDate->modify('-1 day');
echo $oDate->format('Y-m-d 00:00:00');
result
2014-11-05 00:00:00
All the answers here are too long and bloated, everyone loves one-lines ;)
$yesterday = Date('Y-m-d', strtotime('-1 day'));
(Or if you are American you can randomize the date unit order to m/d/y (or whatever you use) and use Cups, galloons, feet and horses as units...)
As of PHP 7 you can write something like this:
$today = new \DateTime();
$yesterday = (clone $today)->modify('-1 day');
$dayBefore = (clone $yesterday)->modify('-1 day');
// Then call ->format('Y-m-d 00:00:00'); on each objects
you can also use new DateTime("now") for today new DateTime("1 day ago") for yesterday or all can be parse by strtotime php function.
Then format as you want.
$timeStamp = time();
// $timeStamp = time() - 86400;
if (date('d.m.Y', $timeStamp) == date('d.m.Y')) {
echo 'Today';
} elseif (date('d.m.Y', $time) == date('d.m.Y', strtotime('-1 day'))) {
echo 'Yesterday';
}
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;