Splitting a start and end date by interval in PHP [duplicate] - php

This question already has answers here:
I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?
(13 answers)
Closed 9 years ago.
Is there any function to split a start and end date into chuncks of $interval days (or months)? For example:
$interval = new DateInterval('P10D');
$start = new DateTime('2012-01-10');
$end = new DateTime('2012-02-16');
$chunks = splitOnInterval($start, $end, $interval);
// Now chunks should contain
//$chunks[0] = '2012-01-10'
//$chunks[1] = '2012-01-20'
//$chunks[2] = '2012-01-30'
//$chunks[3] = '2012-02-09'
//$chunks[3] = '2012-02-16'
I think DatePeriod can help, but i didn't find any way on how i can use it.

Check this article on how to iterate over valid calender days.
In php its something like,
$start = strtotime('2012-01-10');
$end1 = strtotime('2012-02-16');
$interval = 10*24*60*60; // 10 days equivalent seconds.
$chunks = array();
for($time=$start; $time<=$end1; $time+=$interval){
$chunks[] = date('Y-m-d', $time);
}

Here is an example to iterate over days, over month is working accordingly with other interval
<?php
$begin = new DateTime( '2012-11-01' );
$end = new DateTime( '2012-11-11' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
echo $date->format("Y-m-d") . "<br>";
}
?>

Related

How to hide saturday and sunday in PHP array [duplicate]

This question already has answers here:
Get date range between two dates excluding weekends
(4 answers)
Closed 2 years ago.
I want to hide Saturday and Sunday in PHP.
I´ve build the following code:
$begin = new DateTime($row['date']);
$end = new DateTime($row['dateul']);
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
$array[] = $date->format("Y-m-d");
}
Until here the code is working but it outputs the complete week/days in this daterange.
I found this code:
if (strcasecmp($daterange, 'Sun') != 0
&& strcasecmp($daterange, 'Sat') != 0){
}
Do I understand it right, that if value = 1 it will output Saturday for example?
Because the main idea was the following:
if day for example sunday = 0 hide it in array, if sunday=1 show it in array.
The values are coming from MySQL.
You can use the N format for DateTime::format to check the day of week, it returns 6 for Saturday and 7 for Sunday, so as long as the value is less than 6, add it to the array:
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$array = array();
foreach($daterange as $date){
if ($date->format('N') < 6) {
$array[] = $date->format("Y-m-d");
}
}
Demo on 3v4l.org
Update
Based on comments from the OP, days to be included have $row[<dayname>] = 1. In that case, you can use this foreach loop, using l format to get the full day of week name and strtolower to convert to lowercase to use as an index into $row:
foreach($daterange as $date){
if ($row[strtolower($date->format('l'))]) {
$array[] = $date->format("D Y-m-d");
}
}
Demo on 3v4l.org

PHP DatePeriod not showing all months [duplicate]

This question already has answers here:
PHP DateTime::modify adding and subtracting months
(19 answers)
Closed 3 years ago.
When executing the following code, PHP only shows month 10 and 12, ignoring month 11 at all.
$sollStart = '2019-10-31 00:00:01';
$sollEnde = '2019-12-31 23:59:59';
$start = new DateTime($sollStart);
$end = new DateTime($sollEnde);
$periodInterval = new \DateInterval('P1M');
$periodIterator = new \DatePeriod($start, $periodInterval, $end);
foreach ($periodIterator as $monat) {
echo $monat->format('m');
}
If I change the start date to 2019-10-30 it is working like expected.
I've no idea what to change to make it work.
Try this:
$sollstart = (new DateTime('2019-10-31 00:00:01'))->modify('first day of this month');
$sollend = (new DateTime('2019-12-31 23:59:59'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($sollstart, $interval, $sollend);
foreach ($period as $m) {
echo $m->format("m") . "<br>";
}

Get total number of days every 6 months from given total number of years or months

I need to get the total number of days every 6 months from the given total number of days.
So far this is what I achieved.
$begin = new DateTime( '2019-08-31' );
$end = new DateTime( '2020-08-31' );
// get interval in months
$interval1 = DateInterval::createFromDateString('6 month');
$period = new DatePeriod($begin,$interval1,$end);
$counter = 0;
foreach($period as $date){
$counter++;
}
echo "counter:".$counter."<br>";
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$count = 0;
foreach($daterange as $date){
//echo $date->format("Ymd") . "<br>";
$count++;
}
echo "<br>count:".$count;
for($i=1;$i<=$counter;$i++){
}
Actual result:
counter:2
count:366
What I want to achieve:
counter:2
count:366
result:182 // for the first 6 months
result:184 // for the next 6 months
For add month, a start date with day = 31 is a problem. Datetime adds 6 month like this:
echo date_create("2019-08-31")
->modify("+6 Month")
->format("Y-m-d")
; //2020-03-02
If the start is at the beginning of a month, the solution is simple.
With DateTime::diff the difference can be calculated in full days.
$begin = new DateTime( '2019-08-01' );
$end = new DateTime( '2020-08-01' );
$interval = "6 Month";
$counter = 0;
while($begin < $end){
$curStart = clone $begin;
$begin->modify($interval);
$diffDays = $curStart->diff($begin)->days;
echo $curStart->format("Y-m-d")." - ".$begin->format("Y-m-d");
echo ": ".$diffDays." Days<br>";
}
Outputs
2019-08-01 - 2020-02-01: 184 Days
2020-02-01 - 2020-08-01: 182 Days

php get times between tow times like 1:30:00AM

this code get count hours between tow times
ineed out put like this
6:00:00AM
6:30:00AM
7:00:00AM
7:30:00AM
8:00:00AM
8:30:00AM
......
10:30:00PM
how i can do this
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$diff = date_diff($end, $start);
print_r($diff->h);
Alternatively you could use DatePeriod to build the list of dates on the desired specification, as opposed to iterating over comparisons of the date and adding the increment.
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = \DateInterval::createFromDateString('30 minutes');
$periods = new \DatePeriod($start, $interval, $end);
foreach ($periods as $date) {
echo $date->format('h:i:sA') . \PHP_EOL;
}
Result: https://3v4l.org/FnEae
06:00:00AM
06:30:00AM
07:00:00AM
07:30:00AM
08:00:00AM
08:30:00AM
//..
10:30:00PM
Since \DateTime::diff() won't always show the total hours between dates with different days, you can count the hours between the two dates by using iterator_count on the DatePeriod of the desired interval:
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = \DateInterval::createFromDateString('1 hour');
$periods = new \DatePeriod($start, $interval, $end);
echo iterator_count($periods) . ' hours';
Result: https://3v4l.org/P75K7
17 hours
<?php
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$halfHour = new DateInterval('PT1800S');
while ($start < $end) {
print ($start->format('h:i:sA').PHP_EOL);
$start->add($halfHour);
}
?>
Create 2 Datetime objects and a date interval object and iterate while less than the end Datetime add the interval each time.
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = new DateInterval('PT30M');
while ($start <= $end) {
echo $start->format('H:i:s');
$start->add($interval);
}

Get months of certain time period with PHP [duplicate]

This question already has answers here:
How to list all months between two dates
(6 answers)
Closed 7 years ago.
How do I retrieve all the months on 2015-October to 2016-May date range with PHP?
ex - 2015-November, 2015-December, 2016-January.. so on
There is greate example in the PHP docs.
http://php.net/manual/en/class.dateperiod.php
<?php
$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2012-08-31' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
echo $date->format("Ymd") . "<br>";
}
?>
Just provide the needed date format and you are done.
Change it here:
$date->format("Ymd")
As #Uchiha said there is an answer here:
https://stackoverflow.com/a/18743012/2160958

Categories