Good day everyone, I'm trying . This is the code which demonstrates what I'm doing:
<?php
$NoOfGames = 10;
$time = array("06:00:00 " => "07:00:00", "07:00:00" => "08:00:00", "08:00:00" => "09:00:00", "09:00:00" => "10:00:00", "10:00:00" => "11:00:00", "11:00:00" => "12:00:00","12:00:00" => "13:00:00","13:00:00" => "14:00:00","14:00:00" => "15:00:00","15:00:00" => "16:00:00","16:00:00" => "17:00:00","17:00:00" => "18:00:00");
for($i=0;$i<$NoOfGames;$i+1){
$start_time = array_rand($time);
$end_time = $time[$start_time];
$time_new[$start_time] = $end_time;
}
$i =1;
foreach($time_new as $start => $end)
{
echo $i. ") ". $start . " to ". $end . "<br>";
$i++;
}
?>
However, this outputs
and I want to display it like this instead
1) 17:00:00 to 18:00:00
2) 06:00:00 to 07:00:00
3) 12:00:00 to 13:00:00
4) 11:00:00 to 12:00:00
5) 16:00:00 to 17:00:00
6) 08:00:00 to 09:00:00
7) 13:00:00 to 14:00:00
8) 15:00:00 to 16:00:00
9) 14:00:00 to 15:00:00
10) 09:00:00 to 10:00:00
This should work:
Change $i+1 to $i++
The reason is that $i+1 just evaluates to 1 every time (ie doesn't actually increment $i), meaning the loop never exits.
Related
I would like to get dates from tomorrow until 5 days with specify time and date values.
I tried this:
$date = date_create(date('d.m.Y 14:00:00'));
for ($x = 1; $x <= 5; $x++) {
date_add($date, date_interval_create_from_date_string("1 days"));
if ($date->format('w') >= 1 && $date->format('w') <= 5) {
$dates[] = date_format($date, "d.m.Y H:i:s");
}
for ($y = 0; $y < 4; $y++) {
if ($date->format('w') >= 1 && $date->format('w') < 5) {
if ($date->format('H') >= 16) {
$dates[] = date_format($date, "d.m.Y H:i:s");
}
} elseif ($date->format('w') == 5) {
$dates[] = date_format($date, "d.m.Y H:i:s");
}
date_add($date, date_interval_create_from_date_string("1 hours"));
}
date_add($date, date_interval_create_from_date_string("-4 hours"));
}
Result:
Array
(
[0] => 02.07.2021 14:00:00
[1] => 02.07.2021 14:00:00
[2] => 02.07.2021 15:00:00
[3] => 02.07.2021 16:00:00
[4] => 02.07.2021 17:00:00
[5] => 05.07.2021 14:00:00
[6] => 05.07.2021 16:00:00
[7] => 05.07.2021 17:00:00
[8] => 06.07.2021 14:00:00
[9] => 06.07.2021 16:00:00
[10] => 06.07.2021 17:00:00
)
But this is not correct.
It should be:
Array
(
[0] => 02.07.2021 14:00:00
[1] => 02.07.2021 15:00:00
[2] => 02.07.2021 16:00:00
[3] => 02.07.2021 17:00:00
[4] => 02.07.2021 18:00:00
[5] => 05.07.2021 16:00:00
[6] => 06.07.2021 17:00:00
[7] => 06.07.2021 18:00:00
)
Summary:
I would like to get all dates where the day ist NOT saturday or sunday with thime 16,17,18 o'clock. Is it es Friday additional 14 and 15 o'clock.
Where is my mistake ? :(
If you have PHP >= 5.1:
function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}
function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}
Hi i have the following date = 2015-06-01 00:00:00
so i have to add 3 month to this date , so write the following code
$sarting="2015-06-01 00:00:00";
$date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
And i get the output = 2015-09-01;
But now i want to add 3 month again and again for 20 times and i have to store the output to array
so i write the following code
$store_date=array();
for($i=0;$i<20;$i++){
$store_date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
}
But the thing it's returning 2015-09-01 20 times . i need like this 2015-09-01, 2015-12-01,2016-06-01 etc .
Please check
I would recommend using the DateTime-class so we can reuse the same instance on each iteration.
$sarting = "2015-06-01 00:00:00";
$store_date = [];
// Create a DateTime-object we can reuse
$date = new DateTime($sarting);
for ($i = 0; $i < 20; $i++) {
$date->modify('+3 months');
$store_date[] = $date->format('Y-m-d');
}
print_r($store_date);
Since we're resuing the same object, it will keep adding 3 months to it on each iteration. No need for any calculations or anything, making the code clean and readable.
Demo: https://3v4l.org/KZiH9
You can use PHP's dateTime class' modify() method to add +3 months in loop to it.
<?php
$dateStamp = "2015-06-01 00:00:00";
$date = new DateTime($dateStamp);
$datesArr = [];
for ($i=1; $i<21 ; $i++) {
$date->modify('+3 month');
$datesArr[] = $date->format('Y-m-d h:i:s');
}
echo '<pre>';
print_r($datesArr);
echo '</pre>';
Output:
Array
(
[0] => 2015-09-01 12:00:00
[1] => 2015-12-01 12:00:00
[2] => 2016-03-01 12:00:00
[3] => 2016-06-01 12:00:00
[4] => 2016-09-01 12:00:00
[5] => 2016-12-01 12:00:00
[6] => 2017-03-01 12:00:00
[7] => 2017-06-01 12:00:00
[8] => 2017-09-01 12:00:00
[9] => 2017-12-01 12:00:00
[10] => 2018-03-01 12:00:00
[11] => 2018-06-01 12:00:00
[12] => 2018-09-01 12:00:00
[13] => 2018-12-01 12:00:00
[14] => 2019-03-01 12:00:00
[15] => 2019-06-01 12:00:00
[16] => 2019-09-01 12:00:00
[17] => 2019-12-01 12:00:00
[18] => 2020-03-01 12:00:00
[19] => 2020-06-01 12:00:00
)
Working Code:
You are not changing the value of $sarting. Try this:
$sarting = "2015-06-01 00:00:00";
$store_date = array();
for ($i = 0; $i < 20; $i++) {
$sarting = $store_date[$i] = date('Y-m-d', strtotime($sarting . "+3 months"));
}
Try this check if it helps,
$sarting="2015-06-01 00:00:00";
$store_date=array();
for($i=0;$i<20;$i++){
$store_date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
$sarting=$store_date[$i];
}
echo "<pre>";
print_r($store_date);
echo "</pre>";
Below code for Addition or Subtraction for Year/Month/day from date
Addition = date('Y-m-d', strtotime("+0 years +3 months +0 days", strtotime($lastdate)));
Subtraction = date('Y-m-d', strtotime("+0 years -3 months +0 days", strtotime($lastdate)));
$lastdate = "01-12-2015"; //actual date
$after_3_month = date('Y-m-d', strtotime("+0 years +3 months +0 days", strtotime($lastdate))); // adding 3 month on the actual date
echo $after_3_month; //3 month added with actual date
I have trouble to increment time by 15 minutes to end time.
I tried date("H:i:s", strtotime('+15 minutes', strtotime($startTime)));.
But it is not dynamic.
Here i have starttime and endtime.
$startTime = '09:00:00';
$endTime = '11:00:00';
And want to output like,
09:00:00
09:15:00
09:30:00
09:45:00
10:00:00
10:15:00
10:30:00
10:45:00
Thanks.
Please try below code
$startTime='09:00:00';
$endTime='11:00:00';
$Times=array();
$interval=15;
while(strtotime($startTime) < strtotime($endTime))
{
$Times[]=$startTime;
$startTime=strtotime("+".$interval." minutes",strtotime($startTime));
$startTime=date('h:i:s',$startTime);
}
Output
Array
(
[0] => 09:00:00
[1] => 09:15:00
[2] => 09:30:00
[3] => 09:45:00
[4] => 10:00:00
[5] => 10:15:00
[6] => 10:30:00
[7] => 10:45:00
)
I think using do...while you can do this
use this code:
$startTime = '09:00:00';
$endTime = '11:00:00';
$inc = "";
do {
$inc = date("H:i:s", strtotime('+15 minutes', strtotime($startTime)));
$startTime = $inc;
echo $inc." ";
}while($inc < $endTime);
output:
09:15:00 09:30:00 09:45:00 10:00:00 10:15:00 10:30:00 10:45:00 11:00:00
You have to loop over time to generate data you want
$startTime = '09:00:00';
$endTime = '11:00:00';
$new_Time = $startTime;
while($new_Time < $endTime){
$new_Time = date("H:i:s", strtotime('+15 minutes', strtotime($new_Time)));
echo $new_Time;
echo "<br>";
}
o/p:
09:15:00
09:30:00
09:45:00
10:00:00
10:15:00
10:30:00
10:45:00
11:00:00
You can use following codes:
<?php
$startTime = '09:00:00';
$endTime = '11:00:00';
$times = array();
$last_inc = $startTime;
while($last_inc < $endTime) {
$times[] = $last_inc;
$last_inc = date("H:i:s", strtotime("+15 minutes $last_inc"));
}
print_r($times);
Ouput:
Array
(
[0] => 09:00:00
[1] => 09:15:00
[2] => 09:30:00
[3] => 09:45:00
[4] => 10:00:00
[5] => 10:15:00
[6] => 10:30:00
[7] => 10:45:00
)
I have a two dates. start date and end date. Using those dates i have to find recur dates.
example:
(1) start date = 2014-01-01
end date = 2014-05-01
expected output:
2014-01-01
2014-02-01
2014-03-01
2014-04-01
2014-05-01
(2) start date = 2014-01-31
end date = 2014-05-01
expected output:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
means do not skip month, if month is skip in that case use last day of month. how to achive this thing?
I am using following code but i added 1 month in date so it skip the month:
$start = strtotime($start_date);
$end = strtotime($end_date);
$total_dates = array();
for($i=$start; $i<=$end;)
{
$date = date('Y-m-d',$start);
if($i>$start)
{
$next_date = date('Y-m-d',strtotime($date . "+1 month"));
$start = strtotime($next_date);
}
else
{
$next_date = date('Y-m-d',$start);
}
$total_dates[] = $next_date;
$date1 = new DateTime($date);
$newdate = date('Y-m-d',strtotime($date . "+1 month"));
$date2 = new DateTime($newdate);
$diff = $date2->diff($date1)->format("%a");
/* Find diff between two dates */
$i = (($i*1)+(86400*$diff)); //seconds in 1month
}
print_r($total_dates);
so in short:
start date = 2014-01-01
end date = 2014-05-01
expected output:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
current output:
2014-01-31
2015-03-03
2015-04-03
check this
$start_date = '2015-01-31';
$end_date = '2016-06-01';
$start_date=date('Y-m-1', strtotime($start_date));
$diff = abs(strtotime($end_date) - strtotime($start_date));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
if($years>0){$loop=$years*12+$months;}else{$loop=$months;}
for($i=1; $i<=$loop+1;$i++)
{
$total_dates[] =$a= date('Y-m-t', strtotime($start_date));
$start_date = date('Y-m-d',strtotime($start_date . "+1 month"));
}
print_r($total_dates);
//output Array ( [0] => 2015-01-31 [1] => 2015-02-28 [2] => 2015-03-31 [3] => 2015-04-30 [4] => 2015-05-31 [5] => 2015-06-30 [6] => 2015-07-31 [7] => 2015-08-31 [8] => 2015-09-30 [9] => 2015-10-31 [10] => 2015-11-30 [11] => 2015-12-31 [12] => 2016-01-31 [13] => 2016-02-29 [14] => 2016-03-31 [15] => 2016-04-30 [16] => 2016-05-31 [17] => 2016-06-30 )
Try this...
<?php
$start_date = "2014-01-01";
$end_date = "2014-05-01";
$d = date_parse_from_format("Y-m-d", $start_date);
$start= $d["month"];
$d1 = date_parse_from_format("Y-m-d", $end_date);
$end= $d1["month"];
$array=array();
$array[]=date('Y-m-t', strtotime($start_date));
for($i=$start;$i<$end;$i++)
{
$str=explode("-",$start_date);
$mon=$str[1]+$i;
$newdate=$str[0]."-".$mon."-".$str[2];
$array[]=date('Y-m-t', strtotime($newdate));
}
print_r($array);
output:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
2014-05-31
Use PHP date/time functions with relative interval formats:
http://php.net/manual/en/datetime.formats.relative.php
You can provide intervals like "last day of next month" or "last sat of July 2008", adding/subtracting this interval from the referenced date.
I have a PHP script which receives a set of Events from a database with begin/end DateTimes, which represent working times.
Begin | End
2013-08-14 10:00:00 | 2013-08-22 09:30:00
2013-08-08 07:00:00 | 2013-08-08 15:00:00
2013-08-09 07:00:00 | 2013-08-10 07:00:00
Now I want to calculate how much has been worked each single day. For the first row I would want an output like that:
Begin | End
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
....
2013-08-22 00:00:00 | 2013-08-22 09:30:00
I've seen some things with DatePeriod and DateInterval, but those didn't take time into account.
Thanks for your help.
DatePeriod and DateInterval DO take time into account, so you can use those classes.
Your data :
$intervals = [
['begin' => '2013-08-14 10:00:00', 'end' => '2013-08-22 09:30:00'],
['begin' => '2013-08-08 07:00:00', 'end' => '2013-08-08 15:00:00'],
['begin' => '2013-08-09 07:00:00', 'end' => '2013-08-10 07:00:00'],
];
Quick function I wrote :
function explodePeriodByDays($begin, $end) {
$days = [];
$dayInterval = new DateInterval('P1D');
$begin = new DateTime($begin);
$end = new DateTime($end);
$_end = clone $end;
$_end->modify('+1 day');
foreach ((new DatePeriod($begin, $dayInterval, $_end)) as $i => $period) {
$_begin = $period;
if ($i) $_begin->setTime(0, 0, 0);
if ($_begin > $end) break;
$_end = clone $_begin;
$_end->setTime(23, 59, 59);
if ($end < $_end) $_end = $end;
$days[] = [
'begin' => $_begin,
'end' => $_end,
];
}
return $days;
}
Example of function use :
foreach ($intervals as $interval) {
echo "Day intervals from {$interval['begin']} to {$interval['end']} : \n";
foreach (explodePeriodByDays($interval['begin'], $interval['end']) as $day) {
echo "\t {$day['begin']->format('Y-m-d H:i:s')} | {$day['end']->format('Y-m-d H:i:s')}\n";
}
echo "\n";
}
Output of the example :
Day intervals from 2013-08-14 10:00:00 to 2013-08-22 09:30:00 :
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
2013-08-17 00:00:00 | 2013-08-17 23:59:59
2013-08-18 00:00:00 | 2013-08-18 23:59:59
2013-08-19 00:00:00 | 2013-08-19 23:59:59
2013-08-20 00:00:00 | 2013-08-20 23:59:59
2013-08-21 00:00:00 | 2013-08-21 23:59:59
2013-08-22 00:00:00 | 2013-08-22 09:30:00
Day intervals from 2013-08-08 07:00:00 to 2013-08-08 15:00:00 :
2013-08-08 07:00:00 | 2013-08-08 15:00:00
Day intervals from 2013-08-09 07:00:00 to 2013-08-10 07:00:00 :
2013-08-09 07:00:00 | 2013-08-09 23:59:59
2013-08-10 00:00:00 | 2013-08-10 07:00:00
If I am understanding this correctly, you want to get the difference between a begin date and an end date.
I would go about converting the time with a strtotime, subtract the timestamps, then output the date.
$begin = strtotime("2013-08-14 10:00:00");
$end = strtotime("2013-08-14 23:59:59");
$difference = ($end - $begin);
echo gmdate("H:i:s", $difference);
This would give you the time in hours, minutes, then seconds.