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;
?>
Related
What is the best way to compare a month and day date ("m-d") with a date with the format "y-m-d".
for example:
$date1 = "2022-04-20";
$date2 = "2022-05-20";
$seasonStart = "03-15";
$seasonEnd = "04-30";
I want to know how many days between $date1 and $date2 are between $seasonStart and $seasonEnd.
You can use DateTime objects for the comparison.
$date1 = new DateTime($date1);
$date2 = new DateTime($date2);
For the seasonal dates, there are a couple of different ways you can do it depending on how you need it to work.
You can use the year from the given date rather than defaulting to the current year, if you want to see if those dates are within the season for the year in which they occur.
$year = $date1->format('Y');
$seasonStart = new DateTime("$year-$seasonStart");
$seasonEnd = new DateTime("$year-$seasonEnd");
If you only want to compare the date range to the season for the current year, then you can let the year default to the current one.
$seasonStart = DateTime::createFromFormat('m-d H:i', "$seasonStart 00:00");
$seasonEnd = DateTime::createFromFormat('m-d H:i', "$seasonEnd 00:00");
Then you can use this to calculate the number of days in the given range that are included in the season.
if ($date1 > $seasonEnd || $date2 < $seasonStart) {
// in this case the range does not overlap with the season at all
$days = 0;
} else {
$days = (max($seasonStart, $date1))->diff(min($seasonEnd, $date2))->days + 1;
}
<?php
$date = DateTime::createFromFormat('m-d', '02-15');
echo $date->format("Y-m-d");
$season=$date->getTimeStamp();
$current=time();
if ($current>$season) {
echo "Season has started!";
}
?>
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
I am trying to count the number of leap days within a date range.
This is what I have so far (adopted code from here)
<?php
$date_from = strtotime('2019-06-01');
$date_to = strtotime('2021-05-30');
$leapday_count = 0;
for($year=$date_from; $year<=$date_to; $year=strtotime('next year', $year)) {
if (date('L', $year)) {
$leapday_count++;
}
}
echo $leapday_count;
?>
Here is what I need help with:
The above code does only look at the years of the start and end date. For example it does not take into consideration if the start date is after Feb 29th. How can I make sure that it really takes the entire dates into consideration and not only the years of the dates?
Example expected result: 0 leap days between 2020-03-01 2024-02-28
Example expected result: 2 leap days between 2020-02-28 2024-03-01
Thanks in advance!
Stop doing date maths. It's hard.
Use datetime and diff
http://php.net/manual/en/datetime.diff.php
Example there
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
to exactly answer the question.
$start = new DateTime('2020-03-01');
$end = new DateTime('2024-02-28');
$interval = ("last day of feb next year");
$current = clone $start;
$leapYears = 0;
while ($current->modify("last day of feb") && $current <= $end) {
if ($current->format('d') == 29 && $current > $start) {
$leapYears++;
}
$current->modify("+1 year");
}
I'm having trouble getting "weeks" in DateTime::diff function
Here's my code:
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
It worked if I'm going to get the "month" count, but I want to get the weeks before turning it into a month:
We have 4 weeks in a month (4.34524 to be exact from Google Unit Converter), if the difference between start date and the date today exceeds 4 weeks, it should output "1 month" and so on..
Code (Demo):
$date1 = new DateTime("2017-06-1");
$date2 = new DateTime("2017-06-15");
$interval = $date1->diff($date2);
//var_export($interval);
if($interval->m>0){ // check if >= 1 month
echo "{$interval->m} month",($interval->m>1?'s':'');
}else{
$weeks=floor($interval->days/7); // if not yet 1 month, calc weeks
echo "$weeks week",$weeks!=1?'s':'';
}
// output: 2 weeks
Calculate days and then divide by 7 for week.
Try this code :
$date1 = new DateTime("2017-05-14");
$date2 = new DateTime("2017-06-14");
$interval = $date1->diff($date2);
$week = floor($date1->diff($date2)->days/7);
echo $week;
if($week > 4)
{
echo $interval->m.' '.($interval->m > 1 ? 'months' : 'month');
}
you could do this to get weeks and then make the condition:
$daysInAweek = 7;
$weeks = ($interval->days)/$daysInAweek;
if($weeks >= 4) {
echo 'is a month';
}
because a week have 7 days.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many days until XXX date?
Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?
$timezone = "Asia/Dhaka";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=floor($datediff/(60*60*24));
echo "$day_left day(s) left.";
N:B: My timezone is +6 GMT, I mean Asia/Dhaka.
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>