Function calculation date (Except Sunday day, july and August month) - php

Hello I am looking to do a date calculation function. I have a first date for example 2018-06-29 and I would like to add 30 days to this date, if by adding 30 days I fall on a Sunday I would like to withdraw -1 day so fall on a Saturday, the date recover from the calculation will be 2018-07-28 and so on ....
If the date falls in the month of July, we will postpone for the month of June, and if we fall in August, we will postpone the month of September.
Currently I get the first date like this
Array
(
[date_delivery] => 2018-06-27
)
And I prepare the calculation function
public function calculDate($dateDelivery)
{
ddd($dateDelivery);
}
Thank you for your help.

function calcDate($date) {
$newDate = strtotime('+1 month',$date);
$day = day('D',$newDate);
if ($day == 'Sun') $newDate = strtotime('-1 day', $newDate);
}

Related

Get a date and show it even after midnight for about 2 hours

I should be able to get the first Wednesday of each month, I can do it easily using:
$firstWedNextMonth = date ('d-m-Y', strtotime('first wednesday of next month'));
echo "The first wed of next month is: ".$firstWedNextMonth;
However, considering that the first Wednesday of next month is March 6th, the system must show this date until 3:00:00 on Thursday March 7th, at 3:00:01 instead it must indicate Wednesday, April 3rd. I do not know how to do it. Thank you
I think, this should work. +27 hours is 24 hours + your margin (3 hours). You can test it setting $now to whatever you want, instead of the current time.
$now = time();
$firstWedThisMonth = date ('d-m-Y', strtotime('first wednesday of this month', $now));
$firstWedNextMonth = date ('d-m-Y', strtotime('first wednesday of next month', $now));
$boundaryDateTime = strtotime('+27 hours', strtotime($firstWedThisMonth));
if ($now <= $boundaryDateTime) {
echo "The first wed of next month is: ".$firstWedThisMonth;
} else {
echo "The first wed of next month is: ".$firstWedNextMonth;
}

How to get special day of week and list of weekdays using bootstrap datepicker in PHP

I'm using Bootstrap datepicker and I'd like to get special days of every week ( Like: Monday, Wednesday, Saturday). For example, If we select every monday and Wednesday then select only Monday and Wednesday.
And I want this weekday through month wise, (Like, If we select date 24-jun to 24-july and select only Monday. Then every Monday from 24-jun to 24-july will be selected).
So what would be the correct way to get weekdays using bootstrap datepicker?
Any kind of help would be highly appreciated.
Thanks in advance.
I´ve found the solution :
$start_date = "28-06-2016";
$end_date = "28-07-2016";
$weekdays = [1,2]; // 0 = sunday, 1 = monday ...
$range_date = array();
for ($i = strtotime($start_date); $i <= strtotime($end_date); $i = strtotime('+1 day', $i))
{
if(in_array(date('N', $i), $weekdays))//Monday == 1
{
echo date('l Y-m-d', $i).'<br>'; //prints the date only if it's a Monday or tuseday etc..
}
}
And output :
Tuesday 2016-06-28
Monday 2016-07-04
Tuesday 2016-07-05
Monday 2016-07-11
Tuesday 2016-07-12
Monday 2016-07-18
Tuesday 2016-07-19
Monday 2016-07-25
Tuesday 2016-07-26

Repeatable events php

I have array() of week days to repeat my event.
[days_repeat] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
1,2,3 are the events should be repeated every monday, tuesday, wednesday, but I have also
[start_date] => 2014-04-2
[end_date] => 2014-05-30
As you can see start day isn't the first day to repeat, if it was then was easier, just loop and make dates and break loop, when date is bigger than end date, but maybe you can suggest me some good tactics to achieve this
Days repeat array can contain only week day numbers and so!
First, convert start_date to a timestamp.
Then using php date() function as such :
echo date('w', $your_timestamp);
You get to know what day is the day of first_date, 0 (for Sunday) through 6 (for Saturday).
Then just loop and go through your days checking if they match your array.
Try this:
$daysRepeat = array(1,2,3);
$startDate = '2014-04-02';
$endDate = '2014-05-30';
//Convert to timestamps;
$startDateTstamp = strtotime($startDate);
$endDateTstamp = strtotime($endDate);
//Loop through days between start and end date
$tstamp = $startDateTstamp;
while($tstamp < $endDateTstamp) {
//Calculate numerical representation of week day (1-7)
$weekDay = date('N', $tstamp);
if(in_array($weekDay, $daysRepeat)) {
print date('Y-m-d', $tstamp) . "<br />";
}
$tstamp += (60*60*24);
}
Output:
2014-04-02
2014-04-07
2014-04-08
2014-04-09
2014-04-14
2014-04-15
2014-04-16
2014-04-21
2014-04-22
2014-04-23
2014-04-28
2014-04-29
2014-04-30
2014-05-05
2014-05-06
2014-05-07
2014-05-12
2014-05-13
2014-05-14
2014-05-19
2014-05-20
2014-05-21
2014-05-26
2014-05-27
2014-05-28
if your data represents the days within a week (so first day of the week till the seventh day of the week), than you just need to check the current displayed day, if its the same "daynumber"
eg.
echo (new DateTime())->format("w");
will print
1
for monday
see the docs for more informations.
http://www.php.net/manual/en/function.date.php
so youll need only to check, if the current daynumber is in your repeat array!
You can achieve this with this code:
<?php
$days_repeat = array(
'1', // Monday
'2', // Tuesday
'3', // Wednesday
);
// first set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2014-04-02';
// End date
$end_date = '2014-05-30';
while (strtotime($date) <= strtotime($end_date)) {
// first find out which day of the week it is (0=sunday; 1=monday ... 6=saturday)
$dateWeekDay = date("w", $date);
// convert sunday from 0 to 7 if needed
if ($dateWeekDay == 0) { $dateWeekDay = 7; }
// check if $days_repeat has current day
if (in_array($dateWeekDay, $days_repeat)) {
// do action as this day is in $days_repeat
echo date("Y-m-d"). ' is in $days_repeat';
}
// add +1 day and continue loop
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
}
?>
Conveniently, you are representing the day of the week according to the ISO-8601 specification. This means you can use PHP's built-in date() function with no additional translation to your format. That's nice.
Next, you need to figure out how to convert your start_date and end_date into a date format PHP can understand. It looks like mktime() is your best bet here - initialize the time elements to 0 if you don't care about them.
You can now iterate by day and if date("N", $currentDay) is in your array of wanted days, repeat the event on that day (whatever that entails). Here, $currentDay is the looped timestamp you're checking. date("N") returns the same ISO-8601 format day-of-week number as specified in the manual page for date().

PHP date increment by month not exceeding last day of month?

I want same day of month for each of the months that fall between start date and end date. Its just that if the month of the day is not valid for a particular month, you want last day of that month. is there any script?. What is have done is.
$startdate='2010-01-30';
$enddate='2011-01-30';
while ($startdate <= $enddate)
{
echo date('Y-m-d', $startdate ) . "\n";
$startdate = strtotime('+1 month', $startdate);/// for case of feb 28 days last date it should disply as it skips it
}
Expected out put:
For input $startdate='2012-01-30'; $enddate='2013-12-30' Result should be like this ==>
_2012-12-30_
2013-01-30
**2013-02-28**
2013-03-30
2013-04-30
2013-05-30
2013-06-30
2013-07-30
2013-08-30
2013-09-30
2013-10-30
2013-11-30
_2013-12-30_
Loop using for and adding $i months to the start date instead of constantly adding one month to the running value. This way it won't jump to 28th (or 29th) day because of february starting from march.

How to calculate the dates of particular days in the same week number from last year?

I'm aware of the vast amount of date questions on SO but I am stumped at this one.
I need to run some reports and in order to do so I need to work out some dates.
The report will run on a Monday and I need to work out the dates of the Sunday the week before and the following Saturday.
e.g. The report will be run on Monday the 28th May and the dates I need are Sunday the 20th May and Saturday the 26th May.
I then need the same values but for the previous week, so Sunday the 13th May and Saturday the 19th May.
I think this part will be fine as it's just a case of getting the current date of the Monday when the report is run and and manipulating it from there.
THEN finally, the part I can't work out is the first week I mentioned, I need the corresponding dates from last year, so the dates of the Sunday and Saturday from the same week number in the previous year.
I know the date function can give you the week number but can't see how to work out the dates of the Sunday of that week and the previous Saturday based on that.
This is what I have, forgive the OTT variable names:
$today = date('Y/m/d');
// reports run on a Monday and use last Sunday and the following Saturday's date so get yesterday's date first
$yesterday = strtotime ('-1 day', strtotime($today));
$yesterday = date ('Y/m/d', $yesterday);
// start of week (last Sunday)
$start_of_last_week = strtotime ('-1 week', strtotime($yesterday));
$start_of_last_week = date ('Y/m/d', $start_of_last_week);
// end of last week (the following Saturday)
$end_of_last_week = strtotime ('+ 6 days', strtotime($start_of_last_week));
$end_of_last_week = date ('Y/m/d', $end_of_last_week;
// start of previous week
$start_of_previous_week = strtotime ('-1 week', strtotime($start_of_last_week));
$start_of_previous_week = date ('Y/m/d', $start_of_previous_week);
// end of previous week
$end_of_previous_week = strtotime ('+ 6 days', strtotime($start_of_previous_week));
$end_of_previous_week = date ('Y/m/d', previous;
// the start of the same week last year
$start_of_last_week_last_year = strtotime ('-1 year', strtotime($start_of_last_week ));
But the above isn't right so not sure what to do next.
Any help is much appreciated.
You can find out the date of the Monday of a particular year and week number like this
date( 'Y-m-d', strtotime( '2012W21' )); //2011-05-23
Compound Date Formats

Categories