I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?
$begin = new DateTime('2010-05-01');
$end = new DateTime('2010-05-10');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("l Y-m-d H:i:s\n");
}
This will output all days in the defined period between $start
and $end
. If you want to include the 10th, set $end
to 11th. You can adjust format to your liking. See the PHP Manual for DatePeriod. It requires PHP 5.3.
How to iterate between two dates with PHP
Here is the working code for you: https://eval.in/842849
You should use DatePeriod
which takes start-date, date interval, and end-date as arguments.
You will get the result object, which you can loop thru to get the desired dates between the 2 dates:
<?php
$begin = new DateTime('2017-07-18');
$end = new DateTime('2017-08-08');
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date){
echo $date->format("Y-m-d") . "\n";
}
?>
How do I iterate between two dates in php?
Does this work for you?
$date = DateTime::createFromFormat("d-m-Y", '01-01-2017');
$endDate = DateTime::createFromFormat("d-m-Y", '01-06-2017');
while ($date <= $endDate) {
echo $date->format("d/m/Y") . "\n";
$date->modify('+1 day');
}
I tested this at http://sandbox.onlinephpfunctions.com/code/461ae543a882cf8d5e933a4ddc27e8e907f74e01
I have 2 time in PHP, how can I run a foreach loop to go through all of those days? (1 Hour loop same time Last 24 Hours!)
Here is a list of the php documentation you will likely need for this project:
- PHP: Relative Formats
- PHP: Compound Formats
- PHP: strtotime
- PHP: strftime
Here is sample code that will give the last 24 hours in one hour decrements:
$timenow = time();
for ($i = 0; $i < 24; $i ++) {
$ago = strtotime("$i hours ago",$timenow);
echo $ago ," <-- ",strftime('%Y-%m-%d %H:%M:%S',$ago),"\n";
It uses these formats from the relative formats:
number space? (unit | 'week')
Handles relative time items where the value is a number.
"+5 weeks", "12 day", "-7 weekdays"'ago'
Negates all the values of previously found relative time items.
"2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months
ago 5 days", "2 days ago"
Here is sample output:
1554384488 <-- 2019-04-04 09:28:08
1554380888 <-- 2019-04-04 08:28:08
1554377288 <-- 2019-04-04 07:28:08
1554373688 <-- 2019-04-04 06:28:08
1554370088 <-- 2019-04-04 05:28:08
1554366488 <-- 2019-04-04 04:28:08
1554362888 <-- 2019-04-04 03:28:08
1554359288 <-- 2019-04-04 02:28:08
1554355688 <-- 2019-04-04 01:28:08
1554352088 <-- 2019-04-04 00:28:08
1554348488 <-- 2019-04-03 23:28:08
1554344888 <-- 2019-04-03 22:28:08
1554341288 <-- 2019-04-03 21:28:08
1554337688 <-- 2019-04-03 20:28:08
1554334088 <-- 2019-04-03 19:28:08
1554330488 <-- 2019-04-03 18:28:08
1554326888 <-- 2019-04-03 17:28:08
1554323288 <-- 2019-04-03 16:28:08
1554319688 <-- 2019-04-03 15:28:08
1554316088 <-- 2019-04-03 14:28:08
1554312488 <-- 2019-04-03 13:28:08
1554308888 <-- 2019-04-03 12:28:08
1554305288 <-- 2019-04-03 11:28:08
1554301688 <-- 2019-04-03 10:28:08
How can I run a DateTime with for or foreach loop to loop through each day and add +3 days to each?
If the end date is always simply three days from the start, then you only need one loop, over the start date. Then, instead of a second loop to set the ending date, just set it relative to the start, on each iteration:
for ($start = $begin; $start <= $end; $start->modify('+1 day')) {
$stop = clone $start;
$stop->modify('+3 days');
printf("%s => %s\n", $start->format("M d, Y"), $stop->format("M d, Y"));
}
Note you need to make a new date object via clone so that your second modification of +3 days
doesn't change the start date.
PHP - Is there a simple way to loop between two dates and fill in missing values?
Just to demonstrate the power of some of PHP's newer interval handling method (mentioned by pgl in his answer):
$startDate = DateTime::createFromFormat("Y/m/d","2010/12/24",new DateTimeZone("Europe/London"));
$endDate = DateTime::createFromFormat("Y/m/d","2012/01/05",new DateTimeZone("Europe/London"));
$periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
$period = new DatePeriod( $startDate, $periodInterval, $endDate );
foreach($period as $date){
echo $date->format("Y-m-d") , PHP_EOL;
}
Does require PHP >= 5.3.0
EDIT
If you need to include the actual end date, then you need to add a day to $endDate immediately before the foreach() loop:
$endDate->add( $periodInterval );
EDIT #2
$startDate = new DateTime("2010/12/24",new DateTimeZone("Europe/London"));
$endDate = new DateTime("2012/01/05",new DateTimeZone("Europe/London"));
do {
echo $startDate->format("Y-m-d") , PHP_EOL;
$startDate->modify("+1 day");
} while ($startDate <= $endDate);
For PHP 5.2.0 (or earlier if dateTime objects are enabled)
two dates in php, for each loop between dates but reverse?
You could use iterator_to_array + array_reverse methods:
foreach (array_reverse(iterator_to_array($period)) as $day) {...}
Creating an array of dates with a sequence of intervals between days
Carbon is perfectly able to do this:
// $startDate as mentioned should be a valid Carbon date pointed to Tuesday
$dates = [];
for ($currentDate = $startDate; $currentDate <= $endDate; ) {
$dates[] = $currentDate;
$currentDate->addDays(2);
$dates[] = $currentDate;
$currentDate->addDays(2);
$dates[] = $currentDate;
$currentDate->addDay();
}
PHP: Return all dates between two dates in an array
You could also take a look at the DatePeriod class:
$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);
Which should get you an array with DateTime objects.
To iterate
foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}
Related Topics
How to Replace Certain Parts of My String
Download File to Server from Url
How to Call a Function from a String Stored in a Variable
Mcrypt Is Deprecated, What Is the Alternative
How to Generate in PHP All Combinations of Items in Multiple Arrays
How to Find All Youtube Video Ids in a String Using a Regex
How to Update Code That Uses the Deprecated Each() Function
When Should I Use Prepared Statements
Search Form With One or More (Multiple) Parameters
Simplest Way to Profile a PHP Script
Creating the Singleton Design Pattern in PHP5
How to Get a Hex Dump of a String in PHP
Form Inside of $.Load Not Posting Correctly
Apache Shows PHP Code Instead of Executing It
What Is the Advantage of Using Heredoc in PHP
How to Implement an Access Control List in My Web MVC Application