PHP - Get start and end date of current date interval - php

I have as unix timestamps
$now = strtotime("2013-12-10");
$start_date = strtotime("2013-01-01");
$end_date = strtotime("2013-12-31");
The $start date and $end date span a period of time and the $now timestamp sits in the middle of the two.
I also have a variable date interval like so:
$interval = new DateInterval('P1W');
// or
$interval = new DateInterval('P3D');
Given the above how do I get the start and end timestamps of the interval that now sits in?
The $now, $start_date, $end_date and the interval will be dynamic.
Example
Lets say I have these parameters:
$start_date = '2013-01-01 00:00:00';
$end_date = '2013-12-31 23:59:59';
$now = '2013-12-10 15:45:34';
$interval = new DateInterval( 'P1W' );
I want to know the start and end date of the interval $now sits in. The output I would expect from the above params is:
$int_start_date = '2013-12-10 00:00:00';
$int_end_date = '2013-12-16 23:59:59';

I think this is a less hacky and cleaner approach than yours.
$start_date = new DateTime( '2013-01-01 00:00:00' );
$end_date = new DateTime( '2013-12-31 23:59:59' );
$end_date_ts = $end_date->getTimestamp();
$now = new DateTime( '2013-12-10 15:45:34' );
$now_ts = $now->getTimestamp();
$interval = new DateInterval( 'P1W' );
$periods = new DatePeriod( $start_date, $interval, $end_date );
/** #var \DateTime $period */
foreach($periods as $period){
$periodEnd = clone $period;
$periodEnd->add($interval);
if($period < $now && $now < $periodEnd){
$result = iterator_to_array(new \DatePeriod($period, $interval, $periodEnd->add($interval)));
$int_start_date = $result[0];
$int_end_date = $result[1];
break;
}
}
/** #var DateTime $int_start_date */
/** #var DateTime $int_end_date */
var_dump( $int_start_date->format( 'Y-m-d H:i:s' ) );
var_dump( $int_end_date->modify( '-1 Second' )->format( 'Y-m-d H:i:s' ) );

I have figured the problem myself however is hacky
$start_date = new DateTime( '2013-01-01 00:00:00' );
$end_date = new DateTime( '2013-12-31 23:59:59' );
$end_date_ts = $end_date->getTimestamp();
$now = new DateTime( '2013-12-10 15:45:34' );
$now_ts = $now->getTimestamp();
$interval = new DateInterval( 'P1W' );
$period = new DatePeriod( $start_date, $interval, $end_date );
$intervals = array();
foreach ( $period as $dt ) {
$intervals[] = $dt->getTimestamp();
}
$intervals[] = $end_date_ts;
$int_start_date = new DateTime();
$int_end_date = new DateTime();
for ( $i = 0; $i < count( $intervals ); $i++ ) {
if ( $now_ts >= $intervals[$i] && $now_ts <= $intervals[$i+1]) {
$int_start_date->setTimestamp($intervals[$i]);
$int_end_date->setTimestamp($intervals[$i+1]-1);
break;
}
}
var_dump( $int_start_date->format( 'Y-m-d H:i:s' ) );
var_dump( $int_end_date->format( 'Y-m-d H:i:s' ) );
I gladly accept better approaches if anyone has them.

You could try:
$now = time();
$interval = new DateInterval('P1W');
$interval_seconds = $interval->s + ($interval->i * 60) + ($interval->h * 60 * 60) + ($interval->d * 60 * 60 * 24);
$half_interval = round($interval_seconds / 2);
// Unix timestamps
$interval_start = $now - $half_interval;
$interval_end = $now + $half_interval;
EDIT: 2nd answer following on from comments
This only works for intervals of length 1 - eg 1 week, 1 year etc.
If your interval is > 1 then you'll need to somehow determine how far through the interval you are... e.g for a 2 week interval, are you in the first week or the second week?
$now = time();
$interval = "week";
switch ($interval) {
case "year":
$start_int = strtotime(date("Y", $now)."-01-01 00:00:00");
$end_int = strtotime(date("Y", $now)."-12-31 23:59:59");
break;
case "month":
$start_int = strtotime(date("Y-m", $now)."-01 00:00:00");
$end_int = strtotime(date("Y-m-t", $now)." 23:59:59");
break;
case "week":
$start_week = date("Y-m-d", strtotime("previous Monday", $now));
$end_week = date("Y-m-d", strtotime("next Sunday", $now));
$start_int = strtotime($start_week." 00:00:00");
$end_int = strtotime($end_week." 23:59:59");
break;
case "day":
$start_int = strtotime(date("Y-m-d", $now)." 00:00:00");
$end_int = strtotime(date("Y-m-d", $now)." 23:59:59");
break;
case "hour":
$start_int = strtotime(date("Y-m-d H", $now).":00:00");
$end_int = strtotime(date("Y-m-d H", $now).":59:59");
break;
case "minute":
$start_int = strtotime(date("Y-m-d H:i", $now).":00");
$end_int = strtotime(date("Y-m-d H:i", $now).":59");
break;
}
echo date("Y-m-d H:i:s", $start_int), "<br>";
echo date("Y-m-d H:i:s", $end_int), "<br>";
Update 3
I've put the logic from your answer into a single loop:
$start_date = new DateTime( '2013-01-01 00:00:00' );
$end_date = new DateTime( '2013-12-31 23:59:59' );
$end_date_ts = $end_date->getTimestamp();
$now = new DateTime( '2013-12-10 15:45:34' );
$now_ts = $now->getTimestamp();
$interval = new DateInterval( 'P1W' );
$period = new DatePeriod( $start_date, $interval, $end_date );
$int_start_date = $start_date;
$int_end_date = $end_date;
foreach ( $period as $dt ) {
$timestamp = $dt->getTimestamp();
if ($now_ts >= $timestamp) {
$int_start_date->setTimestamp($timestamp);
}
if ($now_ts < $timestamp and $timestamp < $int_end_date->getTimestamp()) {
$int_end_date->setTimestamp($timestamp - 1);
}
}
var_dump( $int_start_date->format( 'Y-m-d H:i:s' ) );
var_dump( $int_end_date->format( 'Y-m-d H:i:s' ) );

Related

Get whole week date starting from sunday to saturday from given date

Hello guys i am working in php and my requirement is to get complete week dates from given date as i need to calculate weekly working hour. And week must be started from sunday to saturday not monday to sunday. I have code which works properly for other days of week except sunday. it means if give any dates from monday to saturday it works properly but if i give sunday's date it give last week's dates. please check my code and advise me for better solution.
$days = array();
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("W");
$y = date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";
for($day=0; $day<=6; $day++)
{
$days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}
print_r($days);
Using the DateTime, DateInterval and DatePeriod classes you could do it like this perhaps
function getperiod( $start ){
return new DatePeriod(
new DateTime( $start ),
new DateInterval('P1D'),
new DateTime( date( DATE_COOKIE, strtotime( $start . '+ 7days' ) ) )
);
}
$start='2018-01-07';
$period=getperiod( $start );
foreach( $period as $date ){
echo $date->format('l -> Y-m-d') . '<br />';
}
Which returns
Sunday -> 2018-01-07
Monday -> 2018-01-08
Tuesday -> 2018-01-09
Wednesday -> 2018-01-10
Thursday -> 2018-01-11
Friday -> 2018-01-12
Saturday -> 2018-01-13
Or, by modifying the parameters of the getperiod function you can make that function far more flexible.
function getperiod( $start, $interval='P1D', $days=7 ){
return new DatePeriod(
new DateTime( $start ),
new DateInterval( $interval ),
new DateTime( date( DATE_COOKIE, strtotime( $start . '+ '.$days.' days' ) ) )
);
}
$start='2018-01-07';
$days=array();
$period=getperiod( $start );
foreach( $period as $date ){
$days[]=$date->format('Y-m-d');
}
echo '<pre>',print_r($days,true),'</pre>';
For instance: To find every Sunday for the next year
$period=getperiod( $start,'P7D', 365 );
foreach( $period as $date ){
$days[]=$date->format('Y-m-d');
}
echo '<pre>',print_r($days,true),'</pre>';
To ensure that the calculations begin on a Sunday which has a numeric value of 7
function getperiod( $start, $interval='P1D', $days=7 ){
return new DatePeriod(
new DateTime( $start ),
new DateInterval( $interval ),
new DateTime( date( DATE_COOKIE, strtotime( $start . '+ '.$days.' days' ) ) )
);
}
/* A date from which to begin calculations */
$start='2018-01-01';
/* Array to store output */
$days=array();
/* integer to represent which day of the week to operate upon */
$startday = 7;
/* Output format for resultant dates */
$output='Y-m-d';
/* Calculate initial startdate given above variables */
$start=date( DATE_COOKIE, strtotime( $start . ' + ' . ( $startday - date( 'N', strtotime( $start ) ) ) . ' days' ) );
/* Get the period range */
$period=getperiod( $start );
foreach( $period as $date ){
/* store output in desired format */
$days[]=$date->format( $output );
}
/* do something with data */
echo '<pre>',print_r($days,true),'</pre>';
From source,
Here is the snippet you are looking for,
// set current date
$date = '01/03/2018';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
print date("m/d/Y l", $ts) . "\n";
}
Here is working demo.
If you need normal standard format code,
Here is your snippet,
// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset * 86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
print date("Y-m-d l", $ts) . "\n";
}
Here is working demo.
EDIT
As per your requirement, now week will start from sunday to saturday
<?php
// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Sunday
$dow = date('w', $ts);
$offset = $dow;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Sunday
$ts = $ts - $offset * 86400;
// loop from Sunday till Saturday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
print date("Y-m-d l", $ts) . "\n";
}
<?php
$days = array();
$ddate = "2018-01-07";
$y = date("Y", strtotime($ddate));
if(date("l", strtotime($ddate))=='Sunday'){
$ddate = date("Y-m-d ", strtotime($ddate. "+1 day"));
}
$date = new DateTime($ddate);
$week = $date->format("W");
echo "<br/>";
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";
for($day=0; $day<=6; $day++)
{
$days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}
print_r($days);
?>
try this:
$days = array();
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("N")==7?$date->modify("+1 week")->format("W"):$date->format("W");
$y = date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";
for($day=0; $day<=6; $day++)
{
$days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}
print_r($days);
$dto = new DateTime();
$year = date_create($this->week_date)->format('o');
$week_no = date('W', strtotime($this->week_date));
$dto->setISODate($year, $week_no);
$dto->modify('-1 days');
$ret['sunday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['monday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['tuesday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['wednesday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['thursday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['friday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['saturday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['next_sunday'] = $dto->format('Y-m-d');

Stepping back in DatePeriod loop

As I understand I can't set the foreach loop back by variable steps, so I use a for loop like this:
$beginDate = new DateTime( $firstday );
$endDate = new DateTime( $lastday );
$endDate = $endDate->modify( '+1 day' );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($beginDate, $interval, $endDate);
$datesArray=array();
foreach($period as $dt) $datesArray[]=$dt->format('Y-m-d');
for ($dateindex=0; $dateindex < count($datesArray); $dateindex++) {
...
Is there a better way to do this?
yes, use
$interval = DateInterval::createFromDateString('-1 day');
then work out the difference in days between $endDate and $beginDate, and use that difference in your loop:
$beginDate = new DateTime('2016-11-28');
$endDate = new DateTime('2016-12-07');
$endDate = $endDate->modify( '+1 day' );
$interval = DateInterval::createFromDateString('-1 day');
$diff = $endDate->diff($beginDate);
$period = new DatePeriod($beginDate, $interval, $diff->days);
$datesArray=array();
foreach($period as $dt) $datesArray[]=$dt->format('Y-m-d');
var_dump($datesArray);

Specific UNIX Timestamps from DateTime in PHP

So, here's the deal. I've been banging my head off of this for the past couple of hours and really haven't made any headway. I've ready through several of these, and even tried uncle google, but no joy.
I have a project I'm working on that deals with a lot of nightclubs and after-hours locations. As a result, we are redefining the day to be from 5:00am-4:59am. I'm having a heck of a time getting those start/end times from the current time. It must be time-zone specific is the part that is screwing me up.
This is ugly, this is after several different failed theories...I'm really chasing my own tail on this one, so any help would be appreciated.
function util_TimeInfo($timezone, $dst){
// Get current DateTime
$dt = new DateTime();
$tz = new DateTimeZone($timezone);
$dt->setTimezone($tz);
// split to variables
$year = $dt->format('Y');
$month = $dt->format('m');
$day = $dt->format('d');
$hour = $dt->format('H');
$minute = $dt->format('i');
$second = $dt->format('s');
// Convert hour to string
$inthour = intval($hr);
// Check which day it is a part of
$mytime[0] = time();
if($inthour < 5){
$start_d = date('d', strtotime('-1 day', $mytime[0]));
$start_m = date('m', strtotime('-1 day', $mytime[0]));
$start_y = date('Y', strtotime('-1 day', $mytime[0]));
$end_d = date('d', $mytime[0]);
$end_m = date('m', $mytime[0]);
$end_y = date('Y', $mytime[0]);
} else{
$start_d = date('d', $mytime[0]);
$start_m = date('m', $mytime[0]);
$start_y = date('Y', $mytime[0]);
$end_d = date('d', strtotime('+1 day', $mytime[0]));
$end_m = date('m', strtotime('+1 day', $mytime[0]));
$end_y = date('Y', strtotime('+1 day', $mytime[0]));
}
// Create current start of day and end of day in unix timestamp
$mytime[1] = mktime('05', '00', '00', $start_m, $start_d, $start_y, $dst);
$mytime[2] = mktime('04', '59', '59', $end_m, $end_d, $end_y, $dst);
//Return times
return $mytime;
}
Using Glavic's code as a base, I was able to get it working how I wanted with an if statement to handle the time between midnight and 5am.
// Get current DateTime
$dt = new DateTime();
$tz = new DateTimeZone($timezone);
$dt->setTimezone($tz);
// Set cutoff for 5am
$cutoff = new DateTime('today 5:00am', $tz);
// Adjust start/end for day
if($dt < $cutoff){
$start = new DateTime('yesterday 5:00:00am', $tz);
$end = new DateTime('today 4:59:59am', $tz);
} else{
$start = new DateTime('today 5:00:00am', $tz);
$end = new DateTime('tomorrow 4:59:59am', $tz);
}
What about this simple code:
$tz = new DateTimezone('Europe/Berlin');
$now = new DateTime('now', $tz);
$start = new DateTime('today 5:00am', $tz);
$end = new DateTime('tomorrow 4:59am', $tz);
if ($now < $start) {
$start = new DateTime('yesterday 5:00am', $tz);
$end = new DateTime('today 4:59am', $tz);
}
demo

How to generate an array of dates with php?

I want to generate an array of dates between two dates with an interval of 1 hour.
Inital date: 01-01-2013 00:00:00
Final date: 02-01-2013 00:00:00
ex. of result:
[01-01-2013 00:00:00, 01-01-2013 01:00:00, 01-01-2013 02:00:00, (...), 02-01-2013 00:00:00]
Try this
$dates = array();
$start = strtotime('01-01-2013 00:00:00');
$end = strtotime('02-01-2013 00:00:00');
for($i=$start;$i<$end;$i+=3600) {
$dates[] = date('Y-m-d H:i:s',$i);
}
$start = new DateTime('2013-07-01 00:00:00', new DateTimeZone('UTC'));
$interval = new DateInterval('PT1H');
$end = new DateTime('2013-07-03 00:00:00', new DateTimeZone('UTC'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
$dateArray[] = $date->format('Y-m-d h:i:s');
}
var_dump($dateArray);
<?php
$start = '2013-01-01 00:00:00';
$end = '2013-01-02 00:00:00';
$dates = array();
$current = strtotime($start);
$offset = 0;
while ( $current < strtotime($end) ) {
$current = strtotime("$start +{$offset} hours");
$dates[] = date('d-m-Y H:i:s', $current);
$offset++;
}
print_r($dates);
You could try this.
$start = mktime(0,0,0,1,1,2013);
$end = mktime(0,0,0,2,1,2013);
$inc = 60*60; // 1 hour
for ($x=$start; $x<=$end; $x+$inc ) {
$dates = date('d-m-Y H:i:s, $x);
}

I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?

I'm starting with a date 2010-05-01 and ending with 2010-05-10. How can I iterate through all of those dates in PHP?
$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.
This also includes the last date
$begin = new DateTime( "2015-07-03" );
$end = new DateTime( "2015-07-09" );
for($i = $begin; $i <= $end; $i->modify('+1 day')){
echo $i->format("Y-m-d");
}
If you dont need the last date just remove = from the condition.
Converting to unix timestamps makes doing date math easier in php:
$startTime = strtotime( '2010-05-01 12:00' );
$endTime = strtotime( '2010-05-10 12:00' );
// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
$thisDate = date( 'Y-m-d', $i ); // 2010-05-01, 2010-05-02, etc
}
When using PHP with a timezone having DST, make sure to add a time that is not 23:00, 00:00 or 1:00 to protect against days skipping or repeating.
Copy from php.net sample for inclusive range:
$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>";
}
Here is another simple implementation -
/**
* Date range
*
* #param $first
* #param $last
* #param string $step
* #param string $format
* #return array
*/
function dateRange( $first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = [];
$current = strtotime( $first );
$last = strtotime( $last );
while( $current <= $last ) {
$dates[] = date( $format, $current );
$current = strtotime( $step, $current );
}
return $dates;
}
Example:
print_r( dateRange( '2010-07-26', '2010-08-05') );
Array (
[0] => 2010-07-26
[1] => 2010-07-27
[2] => 2010-07-28
[3] => 2010-07-29
[4] => 2010-07-30
[5] => 2010-07-31
[6] => 2010-08-01
[7] => 2010-08-02
[8] => 2010-08-03
[9] => 2010-08-04
[10] => 2010-08-05
)
$startTime = strtotime('2010-05-01');
$endTime = strtotime('2010-05-10');
// Loop between timestamps, 1 day at a time
$i = 1;
do {
$newTime = strtotime('+'.$i++.' days',$startTime);
echo $newTime;
} while ($newTime < $endTime);
or
$startTime = strtotime('2010-05-01');
$endTime = strtotime('2010-05-10');
// Loop between timestamps, 1 day at a time
do {
$startTime = strtotime('+1 day',$startTime);
echo $startTime;
} while ($startTime < $endTime);
User this function:-
function dateRange($first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
Usage / function call:-
Increase by one day:-
dateRange($start, $end); //increment is set to 1 day.
Increase by Month:-
dateRange($start, $end, "+1 month");//increase by one month
use third parameter if you like to set date format:-
dateRange($start, $end, "+1 month", "Y-m-d H:i:s");//increase by one month and format is mysql datetime
For Carbon users
use Carbon\Carbon;
$startDay = Carbon::parse("2021-08-01");
$endDay= Carbon::parse("2021-08-05");
$period = $startDay->range($endDay, 1, 'day');
When I print the data
[
Carbon\Carbon #1627790400 {#4970
date: 2021-08-01 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1627876800 {#4974
date: 2021-08-02 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1627963200 {#4978
date: 2021-08-03 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1628049600 {#5007
date: 2021-08-04 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1628136000 {#5009
date: 2021-08-05 00:00:00.0 America/Toronto (-04:00),
},
]
This is Laravel data dump using dd($period->toArray());. You can now iterate through $period if you want with a foreach statement.
One important note - it includes both the edge dates provided to method.
For more cool date related stuff, do check out the Carbon docs.
here's a way:
$date = new Carbon();
$dtStart = $date->startOfMonth();
$dtEnd = $dtStart->copy()->endOfMonth();
$weekendsInMoth = [];
while ($dtStart->diffInDays($dtEnd)) {
if($dtStart->isWeekend()) {
$weekendsInMoth[] = $dtStart->copy();
}
$dtStart->addDay();
}
The result of $weekendsInMoth is array of weekend days!
If you're using php version less than 8.2 and don't have the DatePeriod::INCLUDE_END_DATE const. I wrote a method that returns an array of \DateTimeImmutable.
This works with a start date before, the same or after the end date.
/**
* #param DateTimeImmutable $start
* #param DateTimeImmutable $end
* #return array<\DateTimeImmutable>
*/
public static function getRangeDays(\DateTimeImmutable $start, \DateTimeImmutable $end): array
{
$startDate = $start;
$endDate = $end;
$forwards = $endDate >= $startDate;
$carryDate = $startDate;
$days = [];
while (true) {
if (($forwards && $carryDate > $end) || (!$forwards && $carryDate < $end)) {
break;
}
$days[] = $carryDate;
if ($forwards) {
$carryDate = $carryDate->modify('+1 day');
} else {
$carryDate = $carryDate->modify('- 1 day');
}
}
return $days;
}
$date = new DateTime($_POST['date']);
$endDate = date_add(new DateTime($_POST['date']),date_interval_create_from_date_string("7 days"));
while ($date <= $endDate) {
print date_format($date,'d-m-Y')." AND END DATE IS : ".date_format($endDate,'d-m-Y')."\n";
date_add($date,date_interval_create_from_date_string("1 days"));
}
You can iterate like this also, The $_POST['date'] can be dent from your app or website
Instead of $_POST['date'] you can also place your string here "21-12-2019". Both will work.
<?php
$start_date = '2015-01-01';
$end_date = '2015-06-30';
while (strtotime($start_date) <= strtotime($end_date)) {
echo "$start_daten";
$start_date = date ("Y-m-d", strtotime("+1 days", strtotime($start_date)));
}
?>
If you use Laravel and want to use Carbon the correct solution would be the following:
$start_date = Carbon::createFromFormat('Y-m-d', '2020-01-01');
$end_date = Carbon::createFromFormat('Y-m-d', '2020-01-31');
$period = new CarbonPeriod($start_date, '1 day', $end_date);
foreach ($period as $dt) {
echo $dt->format("l Y-m-d H:i:s\n");
}
Remember to add:
use Carbon\Carbon;
use Carbon\CarbonPeriod;

Categories