How to increment day in loop PHP [duplicate] - php

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 3 years ago.
I have the following code
I get a date begin and date end
$dateBegin = date('2019-11-15');
$dateEnd = date('2019-11-20');
for($i = 0; $i < 5; $i++) {
...
}
//output
2019-11-15 - 2019-11-20
2019-11-21 - 2019-11-26
2019-11-27 - 2019-11-32
2019-11-33 - 2019-11-38
2019-11-39 - 2019-11-44
how to implement a loop add 5 days between begin and end day ?

Use strtotime().
Note that I had to use 6 days to get your desired outcome, not 5.
<?php
$dateBegin = date('2019-11-15');
$dateEnd = date('2019-11-20');
for($i = 0; $i < 5; $i++) {
echo "{$dateBegin} - {$dateEnd}\r\n";
$dateBegin = date('Y-m-d', strtotime($dateBegin. ' + 6 days'));
$dateEnd = date('Y-m-d', strtotime($dateEnd. ' + 6 days'));
}

Related

PHP how many hours are in certain interval [duplicate]

This question already has answers here:
How do I find the hour difference between two dates in PHP?
(10 answers)
Closed 5 years ago.
I have datetimes in the following format:
Start: 2017-07-16 20:00
End: 2017-07-16 23:30
Start: 2017-07-18 21:30
End: 2017-07-19 00:30
I need to tell from these intervals how many hours (in 0,5 increments) are spent between 18:00-22:00 and 22:00-06:00 in total for a month.
Thanks in advance for any hint.
The current code I have is this, but I'm not sure if it is covering all timeframe possibilities:
<?php
date_default_timezone_set("UTC");
$start = array(
"2017-07-16 21:30:00",
"2017-07-16 18:00:00"
);
$end = array(
"2017-07-17 00:30:00",
"2017-07-16 18:30:00"
);
$amount_low = 0;
$amount_high = 0;
for($i = 0; $i < sizeof($start); $i++) {
$start_time = date("H:i:s", strtotime($start[$i]));
$end_time = date("H:i:s", strtotime($end[$i]));
$start_date = date("Ymd", strtotime($start[$i]));
$end_date = date("Ymd", strtotime($end[$i]));
// getting chunk before 22:00 if
if(
(strtotime($start[$i]) >= strtotime($start_date . " 18:00") && strtotime($start[$i]) < strtotime($start_date . " 22:00"))
&&
$start_date < $end_date
) {
$interval_low = strtotime($start_date . " 22:00") - strtotime($start[$i]);
$amount_low += ceil($interval_low / 1800) / 2;
}
//amount_high
if(strtotime($start[$i]) > strtotime($start_date . " 22:00") && strtotime($start[$i]) < strtotime($start_date . " 06:00")) {
$interval_high = strtotime($end[$i]) - strtotime($start[$i]); //needs further things
$amount_high += ceil($interval_high / 1800) / 2;
} elseif (strtotime($start[$i]) < strtotime($start_date . " 22:00") && strtotime($end[$i]) > strtotime($start_date . " 22:00")) {
$interval_high = strtotime($end[$i]) - strtotime($start_date . " 22:00");
$amount_high += ceil($interval_high / 1800) / 2;
} else {
$interval_low = strtotime($end[$i]) - strtotime($start[$i]);
$amount_low += ceil($interval_low / 1800) / 2;
}
}
echo $amount_low;
echo "\n$amount_high";
?>
Would this work for you?
$start = strtotime('2017-07-16 20:00');
$end = strtotime('2017-07-16 23:30');
$interval = $end - $start;
$hours = floor($interval / 1800)/2;
echo($hours);
This will display the total hours (in 0,5 increments) between the two dates (rounding down, so if it's 55 minutes, it's 0,5 hours; replace 'floor' with 'ceil' for the opposite).
I believe a part of your solution is found here from Aillyn
You can convert them to timestamps and go from there:
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Dividing by 3600 because there are 3600 seconds in one hour and using round()
to avoid having a lot of decimal places.
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
This will give you a rounded hour difference between the two times. You could do something similar, just apply it for each interval for which ever month and adjust your math. There's not really going to be a single PHP function you can call to solve this.

Get the day number from a date [duplicate]

This question already has answers here:
PHP - How to get year, month, day from time string
(5 answers)
Closed 7 years ago.
I want to get the day number of a date.
For example my date is: 2015-12-31, it should return 365 and 2015-01-1 would be 1.
How can I do that?
EDIT: date('z') does not work for me, I dont want the current date
Use strtotime() — to parse about any English textual datetime description into a Unix timestamp. And getdate() — Get date/time information.
<?php
$day = getdate(strtotime("2015-12-31"));
echo $day['yday']+1; // output 365
?>
Try
<?php
$today = getdate(strtotime('2015-12-31'));
echo '<pre>';
print_r($today['yday'] + 1);
?>
Read GetDate
You can try it.
<?php
$day = 31;
$mon = 12;
$year = 2015;
$sum = 0;
$days = 0;
$month_day = array(31,28,31,30,31,30,31,31,30,31,30,31);
for ( $i = 0; $i < $mon; $i++){
$sum += $month_day[$i];
}
$days = $sum - ($month_day[$mon-1] - $day);
echo "$days";
?>
You may get day, mon, year from your expected date and make it integer. You should check the year is leap year or not. It may vary one day if it is a leap year. I think you shall do it.

How to get all dates between two dates [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 7 years ago.
I have check-in column and check-out column in my mysql table. I am posting this dates with UI-Datepicker.
eg:
I am trying to block the dates in the datepicker. I have dates like this in database
check in = 2015-06-15
check out = 2015-06-20
Now i want to get dates like this
2015-06-15,
2015-06-16,
2015-06-17,
2015-06-18,
2015-06-19,
2015-06-20
How to get dates like above I mentioned. So please help me and resolve my problem.
<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");
function list_days($date_from,$date_to){
$arr_days = array();
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days
$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
//echo date("F j, Y \n", $day_to_display);
$arr_days[] = date('o-m-d',$day_to_display);
$counter++;
}
return $arr_days;
}
print_r(list_days($date_from,$date_to));
?>

Generate Friday Cutoff on PHP or MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Finding date range for current week, month and year
Can Anyone help me on how could I generate Friday cutoff using PHP or MySQL. below is the sample date range
...
2012-10-06 to 2012-10-12
2012-10-13 to 2012-10-19
2012-10-20 to 2012-10-26
...
2012-12-22 to 2012-12-28
2012-12-29 to 2013-01-04
Knowing the first saturday (the initial period) is easy to calculate the next friday (and looping the next saturday, next friday of second saturday....)
Check this code:
<?php
$saturday = '2012-10-06';
$next_friday = date('Y-m-d', strtotime('+6 days', strtotime($saturday)));
print "The next friday is $next_friday";
Try this code ...
function getCurrentWeek($start , $end){
$ts = strtotime($start);
$te = strtotime($end);
$days = round(abs($te-$ts)/86400);
$range = '';
$j = 0;
// generate dates
for ($i=0; $i<$days; $i++, $ts+=86400){
$temp_date = date("Y-m-d", $ts);
$d = (int) date('w',$ts);
switch ($d) {
case 5 :
if($j == 1)
$range .= $temp_date . "<br />" ;
$j = 0;
break;
case 6 :
$j = 1;
$range .= $temp_date . " TO " ;
break;
}
}
return $range;
}
echo getCurrentWeek('2012-10-06','2013-01-06');
Ps) You need to input correct start and end date where you have a Friday at the end ... or you will have XXXX-XX-XX TO EMPTY

Auto increment years in loop every year [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to increment date with 1 (day/year) in PHP?
Im not really sure where to begin with this but im trying to make a year start at 1928 and stop at 1948 and for every year the years increment by one so since its 2012 the date ranges are 1928 - 1948 and for 2013 it would be 1929 - 1949 and 2014 would be 1930 - 1950 and so on...
right now i just have a basic loop for when to start and stop the years but its not too dynamic, like i said im pretty much at a blank on where to begin other then date('Y')+1.
for($i=1928;$i<=date('Y');$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
if($i == '1948'){break;}
}
So you want it to go between current year minus 84 and the current year minus 64? Use this code:
$firstYear = (int)date('Y') - 84;
$lastYear = $firstYear + 20;
for($i=$firstYear;$i<=$lastYear;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
Edit: updated for performance. Current year is determined before the loop (per Pitchinnate's comment).
Try this:
$year = date('Y');
$add = $year - 2012;
$min = 1928 + $add;
$max = $min + 20;
for($i=$min;$i<=$max;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
I isn't a good idea to have date('Y') or any evaluations done on the for loop as it gets calculated every time through the loop. Article about this.
Something like this?
$base_year = 2012;
$start_year = $base_year - 84;
$end_year = $start_year + 20;
for( $i = $start_year; $i <= $end_year; $i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}
for($i =0; $i <= 20 ;$i++)
{
$year = date('Y') - 84 + $i;
echo '<option value='.$year.'>'.$year.'</option>';
}
It looks like you have a constant with how far back you want the date range to be from the current year that you can use to write a loop that will work for you.
Since it's 2012 now, and you want the range to start at 1928 when it's 2012, then we can use 2012 - 1928 = 84, so the year that starts the range should always be 84 less than the current year.
Therefore we could write code like:
$startingYear = date('Y') - 84;
$endingYear = $startingYear + 20;
for ($i = $startingYear;$i <= $endingYear;$i++)
{
echo '<option value='.$i.'>'.$i.'</option>';
}

Categories