PHP: How calculate calendar days with all 42 days - php

The goal is exclusively to get a range of days, in other words
, start date and end date, as if it were a "calendar matrix", containing the 42 days, being the days of the current month, with the days of the previous month and next month. No need to present (render) a calendar, only get dates.
For example, follow image below.
I need to enter a certain month of a given year, and would need to get this range of days, as picture.
Using PHP Carbon, I easily get the days of the current month, using startOfMonth(), endOfMonth() , subMonth(), addMonth().
Doing this, I get every day of these 3 months, but the goal is to be able to "filter" these days to present only the interval equal to a calendar, but obviously something dynamic, ie, if I use Carbon, would simply inform the desired date , and get "filtered" range, respecting the position of each "cell".
$prev_start = Carbon::now()->subMonth()->startOfMonth();
$prev_end = Carbon::now()->subMonth()->endOfMonth();
$start = Carbon::now()->startOfMonth();
$end = Carbon::now()->endOfMonth();
$next_start = Carbon::now()->addMonth()->startOfMonth();
$next_end = Carbon::now()->addMonth()->endOfMonth();

So here's what you can do:
$monthStart = Carbon::now()->startOfMonth();
$monthEnd = Carbon::now()->endOfMonth();
$calendarStart = $monthStart->startOfWeek(Carbon::SUNDAY);
$calendarEnd = $monthEnd->endOfWeek(Carbon::SATURDAY);
$calendarStart and $calendarEnd should now contain the first and last day that will be displayed in a single screen. This assumes that the calendar will expand the first and last week displayed.
If you are using a calendar that always shows 42 days regardless you can just do:
$monthStart = Carbon::now()->startOfMonth();
$calendarStart = $monthStart->startOfWeek(Carbon::SUNDAY);
$calendarEnd = $calendarStart->addDay(42);

Related

How to find the first day of NEXT month - PHP [duplicate]

This question already has answers here:
How to find first day of the next month and remaining days till this date with PHP
(17 answers)
Closed 2 years ago.
I am working with WooCommerce Subscriptions and am trying to set it so that users can select their own start date using another plugin. Within the date picker plugin, if a user chooses a date within the middle of the month, the payments are supposed to be prorated and then the subscription starts on the first day of the next month. However, the plugin works to only start at the day the subscription starts. I am trying to adjust this so it chooses the start date of the first day of the next month. I have used this as reference - How to find first day of the next month and remaining days till this date with PHP
function woosubscriptions_custom_cart_next_payment_date( $start_pay_date , $recurring_cart){
foreach($recurring_cart->get_cart() as $cart_item_key => $cart_item){
if(isset($cart_item['start-subcription']) && !empty($cart_item['start-subcription'])){
$days_free_trial = WC_Subscriptions_Product::get_trial_length( $cart_item['data'] );
$start_subcription = $cart_item['start-subcription'];
if($days_free_trial > 0){
$start_subcription .= ' ' . $days_free_trial . ' days';
}
$offset=5*60*60; //converting 5 hours to seconds.
$start_pay_date = gmdate( 'Y-m-d H:i:s', strtotime($start_subcription) + $offset);
$first_day_next_month = ($start_pay_date, strtotime('first day of next month'));
break;
}
}
return $first_day_next_month;
}
add_filter( 'wcs_recurring_cart_next_payment_date', 'woosubscriptions_custom_cart_next_payment_date', 10 , 2);
It is not correctly getting the first day of next month.
Your code to calculate the first day of next month is not quite right. The answer you have linked looks different to the code you have used.
strtotime('first day of next month') calculates the first day of the current month and will bear no relation to the start date.
You could use a DateTime object and figure it out from there:
$start_pay_date = new DateTime($start_subcription);
$first_day_next_month = $start_pay_date->modify("first day of next month");
$first_day_next_month would then represent the first day of the month after your $start_pay_date, you can use any of the DateTime methods to then format it how you want (e.g. $first_day_next_month->format("Y-m-d"))

I get an specific day from relative date and also the last day of the relative date, how can I find the difference?

$result = DB::table('customer')->where('isp',2)->get();
// start_date is stored date in database
$date= $result->start_date;
$dts = Carbon::create($date);
// in order to get the last day of relative month
// get the last day of month for instance is equal to 2010-09-30 00:00:00
// find the difference between in days
}
When I use from diffindays of Laravel the output is 0. Any ideas why?

Calculate end date pushing date forward if there is a holiday

I have written some code where I have a start date and number of days duration and then get an end date ie start date 18th December, duration 21 days, end date 8th January. However I want to push the end date forward to avoid certain holidays (25th December through to 6th January) so that the end date becomes 15th January. All the answers I have seen include how to calculate business days and take out weekends, which I don't need. I just want to be able to define specific holidays in an array, get it to see if the holiday is within the start date and end date and if it is move the end date forward by that number of days.
Oh, then the date needs to be inserted into a database. Thanks.
Iterate through the $holidays array. If a holiday is in the given range, then move end date one day in the future:
$startDate = new DateTime('2015-12-18');
$endDate = new DateTime('2016-01-08');
// $holidays array must be sorted. if not, then sort it first
$holidays = array("2015-12-25","2015-12-26","2016-01-01");
$newEndDate = $endDate;
foreach ($holidays as $holiday) {
$holidayDate = new DateTime($holiday);
if ($startDate <= $holidayDate && $holidayDate <= $newEndDate) {
// there is a holiday in the range
$newEndDate->add(new DateInterval('P1D'));
}
}
echo($newEndDate->format('Y-m-d'));
Thank you for all those that posted possible solutions. I solved the problem by adding a selector to the php page which adds 7, 14 or 21 days to the end date (the user sees this as 1 week, 2 weeks etc) and this solved the problem.

Select Mysql with multiple greater and lower values

i am in need of a search query based on 6 values from 3 columns day, month, year, right now i have got it some how working only for month and year but i can not get the day to perform correctly.
for example if some one wants to search from day, month year to day month year.
my current query
//From
list($fy,$fm,$fd) = explode ('-', 2013-2-20);
//to
list($ty,$tm,$td) = explode ('-', 2014-9-1);
$add = " AND
( month >= '".$fm."'
AND year >='".$fy."') AND (
month <= '".$tm."'
AND year <= '".$ty."'
) ";
as you can see the day is not included, your time is much appreciated.
Going on with what you have. You would just add day in the same way you added month and year:
...
$add = " AND
( month >= '".$fm."'
AND year >='".$fy."'
AND day >='".$fd."') AND (
month <= '".$tm."'
AND year <= '".$ty."'
AND day <= '".$td."'
) ";
...
Since all the items are independent of each other, this works of the literal values are higher/lower in each item. Which is probably not what you want. You will need to convert the strings '2013-2-20' into DATE and then use those. This can be done with str_to_date(). So for example str_to_date('2013-2-20','%Y-%m-%d').
Notice: I'm aware that you cannot change the database but for future references it's a good idea to have a single DATE datatype for your database. It's not good design to separate them into separates things such as day, month, and year. As with using the DATE datatype it can easily find lower and higher dates by simply comparing the dates (along with date functions using dates can be used):
dateObj1 >= dateObj2 // dateObj1 is at a later date than dateObj2

PHP DateInterval: Diff between same date in two different months is not one month

I am developing a subscription service where the user pays per month.
I am having two dates
1: 2012-12-05 ($today)
2: 2013-01-05 ($end_date)
For me, that is one month. But for PHP, that is only 30 days.
Here is some code (you can see the values of the dates mapped above)
$end_date_for_new_agents = clone $end_date;
$end_date_for_new_agents = $end_date_for_new_agents->add(new DateInterval("P1D"));
$agent_period_months_obj = $today->diff($end_date_for_new_agents);
echo $agent_period_months_obj->format("%y-%m-%d")
This echos 0-0-30
Why is that and how can I make PHP calculate the difference between the same dates in different months as whole months?
You should condider also the time, or the day can be not complete.
For example:
date_default_timezone_set("Europe/Rome");
$end='2013-01-05';
$intervalo = date_diff(date_create(), date_create($end));
echo $intervalo->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
give as result:
Years:00,Months:00,Days:30,Hours:05,Minutes:59,Seconds:53
The following code
date_default_timezone_set("Europe/Rome");
$start='2012-12-05';
$end='2013-01-05';
$intervalo = date_diff(date_create($start), date_create($end));
echo $intervalo->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
give the result:
Years:00,Months:01,Days:0,Hours:00,Minutes:0,Seconds:0
Check if value of$today has also the time, if so you should extract only the date and forget the time

Categories