Get months of certain time period with PHP [duplicate] - php

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

Related

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>";
}

Printing dates between two other dates on PHP [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 6 years ago.
I'm trying to print dates between two other dates. Here is my code:
$begin = date("d/m/y");
$end = date("d/m/y", strtotime("+1 month"));
$i = 0;
while( strtotime($begin) <= strtotime($end) ){
echo "$begin\n";
$i++;
$begin = date("d/m/y", strtotime("+$i day") );
}
You can execute the same code here:
http://sandbox.onlinephpfunctions.com/code/34c4b721553038f585806798121941bee0c66086
For some reason this code is printing just the dates between 25/01/2017 and 31/01/2017 instead of 25/01/2017 and 25/02/2017. I don't know what's wrong. Can someone help me?
strtotime() doesn't support dates in d/m/y format. It treats these dates as m/d/y.
To fix your code, use Y-m-d format in the first two lines.
On a sidenote, I'd recommend to use \DateTime classes to manipulate dates instead of strings and integers. Read more here: https://paulund.co.uk/datetime-php
<?php
error_reporting(-1);
ini_set('display_errors', true);
$begin = new DateTime();
$end = (new DateTime())->modify('+1 month');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $date) {
echo $date->format('d/m/y')."<br/>";
}

How to check if there is Feb 29 in between two dates using php [duplicate]

This question already has answers here:
Checking if there's a leap day in a timerange
(2 answers)
Closed 7 years ago.
How to check if there is Feb 29 in between two dates using php
$fromDate = strtotime($contract->from);
$toDate = strtotime($contract->to);
DateTime, DateInterval and DatePeriod is a good way to work with dates in PHP:
$begin = new DateTime( '2015-12-20' );
$end = new DateTime( '2016-03-10' );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ) {
if(($dt->format('m') === '02') && ($dt->format('d') === '29')) {
echo 'got date February 29th!';
}
}
This script will take first date, last date and loop over the interval day by day to check for the desired date.

get all weekdays in php [duplicate]

This question already has an answer here:
PHP: getting weekdays numbers of a given month
(1 answer)
Closed 10 years ago.
I am thingking if how to get all the date of weekdays between a given date
example: I have a date given 2013-01-01 and 2013-20-01
It must return all date of weekdays like 2013-01-01
how could this be done using php
thankz
Look into DatePeriod Class, this is available from PHP 5.3.
Here is an example from the site
$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){
// You can manipulate the date here
echo $date->format("Ymd") . "<br>";
}
You can get the weekday from the timestamp using the multitude of formatting options avalaible:
echo date("D",strtotime("2013-01-01"));
tues

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

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>";
}
?>

Categories