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
Related
This question already has answers here:
Day difference without weekends
(12 answers)
Closed 5 years ago.
In php i am using this code to calculate difference between two dates in number of days.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
I am getting the result, but is there any way to skip Saturday and Sunday if they are in between these dates.
See I Do this And Its Working Properly
You Can Try This.
$day = date('l',strtotime($datte));
if($day == 'Saturday' || $day == 'Sunday')
{
//SOME ACTION
}else{
//SOME ACTION
}
Where L is Define Day Full Name.
I create an array with all the date numeric representation of the day (0-6 for sun-sat).
And if it's between 1-5 (mon-fri) add it in the array.
The count of the array is then the number of days without sat-sundays.
<?php
$startdate = '2017-08-04';
$datetime1 = new DateTime($startdate);
$datetime2 = new DateTime('2017-08-07');
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');
$arr =array();
for($i=0;$i<=$days;$i++){
$day = date("w Y-m-d l", strtotime($startdate) + $i*86400);
if((int)$day[0] >= 1 && (int)$day[0] <= 5 ) $arr[] = $day;
}
var_dump($arr);
echo "days excluding sat-sundays " . count($arr);
https://3v4l.org/pPLpb
Edit added = to count the end date also.
Edit; there was something not working with my previous strtotime, it gave correct resultat but counted Tuesdays as Mondays.
Changed it to $i*86400 instead and added more data in the array for debugging.
holidayes hold saturday and sunday counts;
w hold distance without holidays
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-12-13');
$interval = $datetime1->diff($datetime2);
$distance=$interval->format('%R%a');
$hoildays=($distance/7)*2;// *2 cuz saturday and sunday
$W=$distance-($distance/$hoildays);
echo $W;
?>
I am trying to get the date of every monday skipping a month each time but I keep getting only the monday of the first week. This is my code:
$begin = new \DateTime("2017-04-01");
$end = new \DateTime("2017-08-31");
$interval = \DateInterval::createFromDateString("next monday of +2 months");
$period = new \DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
{
// echo the date for each monday found in the second month
}
Any help would be greatly appreciated.
You seem to have mixed the $begin and $end values. Also, the DateInterval will only have one Monday every single month. I was not able to find a interval expression to get every Monday in every other month, so I did the filtering manually. In this example we use the month from the begin date, and includes the Mondays from that date, skip two months and so on.
<?php
$end = new \DateTime("2017-04-01");
$begin = new \DateTime("2007-08-31");
$month_even = ((int) $begin->format('m')) % 2 === 0;
$interval = \DateInterval::createFromDateString("next monday");
$period = new \DatePeriod($begin, $interval, $end);
foreach ($period as $dt) {
// Check if we want to show even months, make sure that the current month is even
// or, if we want to show odd months, the month should be odd.
if ((((int) $dt->format('m')) % 2 === 0) === $month_even) {
echo $dt->format('d-m-Y') . PHP_EOL;
}
}
Outputs:
31-08-2007
01-10-2007
[...]
06-02-2017
13-02-2017
20-02-2017
27-02-2017
How to check if month exist in specific period ($date1 and $date2)
$month = '2016-01';
$date1 = '2016-01-05';
$date2 = '2016-02-04;
First convert the month into a date, like the first day of the month. Then you can compare the dates to check if the month lies in between:
$month_day = date ('Y-m-01', strtotime($month) );
$date1_day = date ('Y-m-01', strtotime($date1) );
$date2_day = date ('Y-m-01', strtotime($date2) );
if ( ($month_day >= min($date1_day, $date2_day))
&& ($month_day <= max($date1_day, $date2_day)) )
{ }
I got a best answer for my question on his link The Answer
This answer get months between two dates
$start = (new DateTime('2016-01-05'))->modify('first day of this month');
$end = (new DateTime('2016-02-04'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$monthsArray = [];
foreach ($period as $dt) {
$monthsArray[] = $dt->format("Y-m"); // I put months exist in this period on array to check later if the $month exist on this array or not
}
You could search the strings using strpos(). http://php.net/manual/en/function.strpos.php
ex:
if (strpos($date1,$month) || strpos($date2,$month)){/* do stuff */}
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.
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>";
}
?>