How to get start and end of the week between two dates - php

I have two dates
$start_date = '2015-09-21';
$end_Date = '2016-09-21';
What i want to get like this
Week = Monday Friday
36 = 2015-09-21 2015-09-25
37 = 2015-09-28 2015-10-02
38 = 2015-10-05 2015-10-09
.
.
38 = 2016-09-19 2016-09-24
The date difference may be of one year or more. What i need to get is exact each week Monday and Friday dates between this time interval.
I used this method
$weeks = array();
while ($start_date < $end_Date )
{
$weeks[] = date('W', $start_date );
$start_date += strtotime('+1 week', 0);
}
function getStartAndEndDate($week, $year)
{
$time = strtotime("1 January $year", time());
$day = date('w', $time);
$time += ((7*$week)+1-$day)*24*3600;
$return[0] = date('Y-m-d', $time);
$time += 6*24*3600;
$return[1] = date('Y-m-d', $time);
return $return;
}
When i call this function, I need to put week number and year to get exact dates. But I cant get year with the particular week. I can managed to get week from the start date or end date.
$current_year = date("Y", strtotime($fromdate));
Any one suggest me to get the exact year with the week number

First of all you need to convert the time into object, after that calculate the difference. Now calculate the weeks between the given dates. Now its time loop through the weeks from start date, I use DateInterval('P4D') to ahead the date by 4 days and then echo two dates then again DateInterval('P3D') to complete the week and this is repeated.
$start_date = '2015-09-21';
$end_Date = '2016-09-21';
$date1 = new DateTime($start_date);
$date2 = new DateTime($end_Date);
$interval = $date1->diff($date2);
$weeks = floor(($interval->days) / 7);
for($i = 1; $i <= $weeks; $i++){
$week = $date1->format("W");
$date1->add(new DateInterval('P4D'));
echo $week." = ".$start_date." - ".$date1->format('Y-m-d')."<br/>";
$date1->add(new DateInterval('P3D'));
$start_date = $date1->format('Y-m-d');
}
Output:
39 = 2015-09-21 - 2015-09-25
40 = 2015-09-28 - 2015-10-02
41 = 2015-10-05 - 2015-10-09
42 = 2015-10-12 - 2015-10-16
43 = 2015-10-19 - 2015-10-23
44 = 2015-10-26 - 2015-10-30
45 = 2015-11-02 - 2015-11-06
46 = 2015-11-09 - 2015-11-13
47 = 2015-11-16 - 2015-11-20
48 = 2015-11-23 - 2015-11-27
49 = 2015-11-30 - 2015-12-04
50 = 2015-12-07 - 2015-12-11
51 = 2015-12-14 - 2015-12-18
52 = 2015-12-21 - 2015-12-25
53 = 2015-12-28 - 2016-01-01
01 = 2016-01-04 - 2016-01-08
02 = 2016-01-11 - 2016-01-15
03 = 2016-01-18 - 2016-01-22
04 = 2016-01-25 - 2016-01-29
05 = 2016-02-01 - 2016-02-05
06 = 2016-02-08 - 2016-02-12
07 = 2016-02-15 - 2016-02-19
08 = 2016-02-22 - 2016-02-26
09 = 2016-02-29 - 2016-03-04
10 = 2016-03-07 - 2016-03-11
11 = 2016-03-14 - 2016-03-18
12 = 2016-03-21 - 2016-03-25
13 = 2016-03-28 - 2016-04-01
14 = 2016-04-04 - 2016-04-08
15 = 2016-04-11 - 2016-04-15
16 = 2016-04-18 - 2016-04-22
17 = 2016-04-25 - 2016-04-29
18 = 2016-05-02 - 2016-05-06
19 = 2016-05-09 - 2016-05-13
20 = 2016-05-16 - 2016-05-20
21 = 2016-05-23 - 2016-05-27
22 = 2016-05-30 - 2016-06-03
23 = 2016-06-06 - 2016-06-10
24 = 2016-06-13 - 2016-06-17
25 = 2016-06-20 - 2016-06-24
26 = 2016-06-27 - 2016-07-01
27 = 2016-07-04 - 2016-07-08
28 = 2016-07-11 - 2016-07-15
29 = 2016-07-18 - 2016-07-22
30 = 2016-07-25 - 2016-07-29
31 = 2016-08-01 - 2016-08-05
32 = 2016-08-08 - 2016-08-12
33 = 2016-08-15 - 2016-08-19
34 = 2016-08-22 - 2016-08-26
35 = 2016-08-29 - 2016-09-02
36 = 2016-09-05 - 2016-09-09
37 = 2016-09-12 - 2016-09-16

$start_date = '2015-09-21';
$end_Date = '2016-09-21';
$startTime = strtotime($start_date);
$endTime = strtotime($end_Date);
$weeks = array();
$date = new DateTime();
$i=0;
while ($startTime < $endTime) {
$weeks[$i]['week'] = date('W', $startTime);
$weeks[$i]['year'] = date('Y', $startTime);
$date->setISODate($weeks[$i]['year'], $weeks[$i]['week']);
$weeks[$i]['Monday']=$date->format('Y-m-d');
$weeks[$i]['Friday'] = date('Y-m-d',strtotime($weeks[$i]['Monday'] . "+4 days"));
$startTime += strtotime('+1 week', 0);
$i++;
}
var_dump($weeks);

Related

Need 10 intervals between two time stamps

I need 10 intervals between two time stamps. Please find the example below.
start_date: 2021-01-15 15:45:00
end_date: 2021-01-15 18:00:00
I need result like below based on count of intervals. I can change 5 interval or 7 intervals but the time need to calculate automatically based on intervals
15:45:00
16:00:00
16:15:00
16:30:00
16:45:00
17:00:00
17:15:00
17:30:00
17:45:00
18:00:00
function SplitTime($StartTime, $EndTime, $Duration="60"){
$ReturnArray = array ();// Define output
$StartTime = strtotime ($StartTime); //Get Timestamp
$EndTime = strtotime ($EndTime); //Get Timestamp
$AddMins = $Duration * 60;
while ($StartTime <= $EndTime) //Run loop
{
$ReturnArray[] = date ("G:i:s", $StartTime);
$StartTime += $AddMins; //Endtime check
}
return $ReturnArray;
}
You can use a division of the number of seconds between the 2 dates :
function SplitTime($StartTime, $EndTime, $Division = 10){
$ReturnArray = array (); // Define output
$StartTime = strtotime ($StartTime); //Get Timestamp
$EndTime = strtotime ($EndTime); //Get Timestamp
//Time diff
$diff = $EndTime - $StartTime;
// number of seconds by "step"
$num = $diff / ($Division - 1);
for ($inum = 0; $inum < $Division; $inum++)
{
$ReturnArray[] = date ("G:i:s", $StartTime + $num * $inum);
}
return $ReturnArray;
}
$dates = SplitTime("2021-01-15 15:45:00", "2021-01-15 18:00:00");
print_r($dates);
Outputs :
Array
(
[0] => 15:45:00
[1] => 16:00:00
[2] => 16:15:00
[3] => 16:30:00
[4] => 16:45:00
[5] => 17:00:00
[6] => 17:15:00
[7] => 17:30:00
[8] => 17:45:00
[9] => 18:00:00
)
See online demo

Add 3 month to the given date in a loop PHP

Hi i have the following date = 2015-06-01 00:00:00
so i have to add 3 month to this date , so write the following code
$sarting="2015-06-01 00:00:00";
$date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
And i get the output = 2015-09-01;
But now i want to add 3 month again and again for 20 times and i have to store the output to array
so i write the following code
$store_date=array();
for($i=0;$i<20;$i++){
$store_date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
}
But the thing it's returning 2015-09-01 20 times . i need like this 2015-09-01, 2015-12-01,2016-06-01 etc .
Please check
I would recommend using the DateTime-class so we can reuse the same instance on each iteration.
$sarting = "2015-06-01 00:00:00";
$store_date = [];
// Create a DateTime-object we can reuse
$date = new DateTime($sarting);
for ($i = 0; $i < 20; $i++) {
$date->modify('+3 months');
$store_date[] = $date->format('Y-m-d');
}
print_r($store_date);
Since we're resuing the same object, it will keep adding 3 months to it on each iteration. No need for any calculations or anything, making the code clean and readable.
Demo: https://3v4l.org/KZiH9
You can use PHP's dateTime class' modify() method to add +3 months in loop to it.
<?php
$dateStamp = "2015-06-01 00:00:00";
$date = new DateTime($dateStamp);
$datesArr = [];
for ($i=1; $i<21 ; $i++) {
$date->modify('+3 month');
$datesArr[] = $date->format('Y-m-d h:i:s');
}
echo '<pre>';
print_r($datesArr);
echo '</pre>';
Output:
Array
(
[0] => 2015-09-01 12:00:00
[1] => 2015-12-01 12:00:00
[2] => 2016-03-01 12:00:00
[3] => 2016-06-01 12:00:00
[4] => 2016-09-01 12:00:00
[5] => 2016-12-01 12:00:00
[6] => 2017-03-01 12:00:00
[7] => 2017-06-01 12:00:00
[8] => 2017-09-01 12:00:00
[9] => 2017-12-01 12:00:00
[10] => 2018-03-01 12:00:00
[11] => 2018-06-01 12:00:00
[12] => 2018-09-01 12:00:00
[13] => 2018-12-01 12:00:00
[14] => 2019-03-01 12:00:00
[15] => 2019-06-01 12:00:00
[16] => 2019-09-01 12:00:00
[17] => 2019-12-01 12:00:00
[18] => 2020-03-01 12:00:00
[19] => 2020-06-01 12:00:00
)
Working Code:
You are not changing the value of $sarting. Try this:
$sarting = "2015-06-01 00:00:00";
$store_date = array();
for ($i = 0; $i < 20; $i++) {
$sarting = $store_date[$i] = date('Y-m-d', strtotime($sarting . "+3 months"));
}
Try this check if it helps,
$sarting="2015-06-01 00:00:00";
$store_date=array();
for($i=0;$i<20;$i++){
$store_date[$i]=date('Y-m-d', strtotime($sarting . "+3 months") );
$sarting=$store_date[$i];
}
echo "<pre>";
print_r($store_date);
echo "</pre>";
Below code for Addition or Subtraction for Year/Month/day from date
Addition = date('Y-m-d', strtotime("+0 years +3 months +0 days", strtotime($lastdate)));
Subtraction = date('Y-m-d', strtotime("+0 years -3 months +0 days", strtotime($lastdate)));
$lastdate = "01-12-2015"; //actual date
$after_3_month = date('Y-m-d', strtotime("+0 years +3 months +0 days", strtotime($lastdate))); // adding 3 month on the actual date
echo $after_3_month; //3 month added with actual date

PHP - Auto calculate time intervals in laravel

I have three columns in my table start_time, end_time and interval.
The start_time and end_time stores time like 22:00 and 24:00 respectively and the interval store time like 00:15:00.
What I want to do is auto calculate intervals like 22:00 - 22:15, 22:15 - 22:30 and so on till 23:45 - 24:00.
And if the interval is 00:30:00 the intervals should be 22:00 - 22:30.
I'm new to laravel and stackoverflow so sorry for any inconvenience.
You can use for loop together with strtotime.
$start= "20:00";
$end = "22:00";
$int = 15;
$int *= 60;
$start = strtotime($start);
$end = strtotime($end);
for($i = $start; $i < $end;){
$res[] = date("H:i", $i) . "-" . date("H:i", $i + $int);
$i += $int;
}
var_dump($res);
https://3v4l.org/I2lUS
EDIT; didn't notice you wanted a different output
<?php
$begin = new DateTimeImmutable('19:00');
$end = new DateTimeImmutable('24:30'); // Last 30 min will not be inclued (see output)
$interval = new DateInterval('PT30M'); // Interval 30 min
$daterange = new DatePeriod($begin, $interval ,$end);
$ranges = [];
foreach ($daterange as $key => $range) {
$ranges[$key] = $range->format('H:i');
if($key>0) {
$ranges[$key-1] .= ' - '.$range->format('H:i');
}
}
array_pop($ranges);
print_r($ranges);
Output:
Array
(
[0] => 19:00 - 19:30
[1] => 19:30 - 20:00
[2] => 20:00 - 20:30
[3] => 20:30 - 21:00
[4] => 21:00 - 21:30
[5] => 21:30 - 22:00
[6] => 22:00 - 22:30
[7] => 22:30 - 23:00
[8] => 23:00 - 23:30
[9] => 23:30 - 00:00
)

PHP DateInterval Jan - June and July - Dec

Trying to create a list or date interval for the past 2 years, divided into ranges of 6 months, i.e. Jan - June 2015, July - Dec 2015, Jan - June 2016 ... until today.
I have looked into DateTime and DateInterval, something like this
$begin = new DateTime( '2015-01-01' );
$end = new DateTime( '2017-12-26' );
$interval = new DateInterval('P6M');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
echo sprintf("%s\n", $dt->format("Y-m-d"));
Which gives me
2015-01-01
2015-07-01
2016-01-01
2016-07-01
2017-01-01
2017-07-01
Can't figure out how to get start and end (Jan 1 - June 30 and July 1 - Dec 31) for each period.
After adding an interval, subtract one day from it. Use sub() and new DateInterval('P1D'), which indicates subtraction of one day.
foreach ( $period as $dt )
echo sprintf(
"%s %s\n",
$dt->format("Y-m-d"),
$dt->add($interval)
->sub(new DateInterval('P1D'))
->format("Y-m-d"));
This outputs:
2015-01-01 2015-06-30
2015-07-01 2015-12-31
2016-01-01 2016-06-30
2016-07-01 2016-12-31
2017-01-01 2017-06-30
2017-07-01 2017-12-31
Update: In the comments you also asked how to indicate what half of the year the date represents. The answer is too obvious:
sprintf(
"%s %s %d\n",
$dt->format("Y-m-d"),
$dt->add($interval)
->sub(new DateInterval('P1D'))
->format("Y-m-d"),
$dt->format("n")/6
);
Note %d and corresponding $dt->format("n")/6.
Output for me:
2015-01-01 2015-06-30 1
2015-07-01 2015-12-31 2
2016-01-01 2016-06-30 1
2016-07-01 2016-12-31 2
2017-01-01 2017-06-30 1
2017-07-01 2017-12-31 2
you can just add the interval size '-1' for each start like this:
$begin = new DateTime( '2015-01-01' );
$end = new DateTime( '2017-12-26' );
$interval = new DateInterval('P6M');
$period = new DatePeriod($begin, $interval, $end);
$oneDay = new DateInterval("P1D");
$oneDay->invert=1; #just get the previous day as if("P-1D")
foreach ( $period as $dt ){
echo $dt->format("Y-m-d"); #interval start
$dt->add($interval); #adding the interval size
$dt->add($oneDay); #get the last day of the cur interval
echo "<br>";
echo $dt->format("Y-m-d"); #interval end
echo "<br>";
}

Monthly recur with PHP

I have a two dates. start date and end date. Using those dates i have to find recur dates.
example:
(1) start date = 2014-01-01
end date = 2014-05-01
expected output:
2014-01-01
2014-02-01
2014-03-01
2014-04-01
2014-05-01
(2) start date = 2014-01-31
end date = 2014-05-01
expected output:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
means do not skip month, if month is skip in that case use last day of month. how to achive this thing?
I am using following code but i added 1 month in date so it skip the month:
$start = strtotime($start_date);
$end = strtotime($end_date);
$total_dates = array();
for($i=$start; $i<=$end;)
{
$date = date('Y-m-d',$start);
if($i>$start)
{
$next_date = date('Y-m-d',strtotime($date . "+1 month"));
$start = strtotime($next_date);
}
else
{
$next_date = date('Y-m-d',$start);
}
$total_dates[] = $next_date;
$date1 = new DateTime($date);
$newdate = date('Y-m-d',strtotime($date . "+1 month"));
$date2 = new DateTime($newdate);
$diff = $date2->diff($date1)->format("%a");
/* Find diff between two dates */
$i = (($i*1)+(86400*$diff)); //seconds in 1month
}
print_r($total_dates);
so in short:
start date = 2014-01-01
end date = 2014-05-01
expected output:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
current output:
2014-01-31
2015-03-03
2015-04-03
check this
$start_date = '2015-01-31';
$end_date = '2016-06-01';
$start_date=date('Y-m-1', strtotime($start_date));
$diff = abs(strtotime($end_date) - strtotime($start_date));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
if($years>0){$loop=$years*12+$months;}else{$loop=$months;}
for($i=1; $i<=$loop+1;$i++)
{
$total_dates[] =$a= date('Y-m-t', strtotime($start_date));
$start_date = date('Y-m-d',strtotime($start_date . "+1 month"));
}
print_r($total_dates);
//output Array ( [0] => 2015-01-31 [1] => 2015-02-28 [2] => 2015-03-31 [3] => 2015-04-30 [4] => 2015-05-31 [5] => 2015-06-30 [6] => 2015-07-31 [7] => 2015-08-31 [8] => 2015-09-30 [9] => 2015-10-31 [10] => 2015-11-30 [11] => 2015-12-31 [12] => 2016-01-31 [13] => 2016-02-29 [14] => 2016-03-31 [15] => 2016-04-30 [16] => 2016-05-31 [17] => 2016-06-30 )
Try this...
<?php
$start_date = "2014-01-01";
$end_date = "2014-05-01";
$d = date_parse_from_format("Y-m-d", $start_date);
$start= $d["month"];
$d1 = date_parse_from_format("Y-m-d", $end_date);
$end= $d1["month"];
$array=array();
$array[]=date('Y-m-t', strtotime($start_date));
for($i=$start;$i<$end;$i++)
{
$str=explode("-",$start_date);
$mon=$str[1]+$i;
$newdate=$str[0]."-".$mon."-".$str[2];
$array[]=date('Y-m-t', strtotime($newdate));
}
print_r($array);
output:
2014-01-31
2014-02-28
2014-03-31
2014-04-30
2014-05-31
Use PHP date/time functions with relative interval formats:
http://php.net/manual/en/datetime.formats.relative.php
You can provide intervals like "last day of next month" or "last sat of July 2008", adding/subtracting this interval from the referenced date.

Categories