PHP - How to calculate one month or one year later - php

I'd like to calculate next billing date of Recurly plan in PHP.
There are 2 types of billing cycle: yearly | monthly.
I tried to use DateTime and DateInterval classes, but didn't get expected results.
<?php
$referenceTime = new \DateTime('2016-01-31');
$oneMonthLater = $referenceTime->modify('+1 month');
var_dump($oneMonthLater);
// public 'date' => string '2016-03-02 00:00:00.000000'
Adding one month to the 31st of Januray gives me the second of March and not the 29th (or 28th) of February as I would expect.
The same for the 31st of August:
<?php
$referenceTime = new \DateTime('2016-08-31');
$oneMonthLater = $referenceTime->modify('+1 month');
var_dump($oneMonthLater);
// public 'date' => string '2016-10-01 00:00:00.000000'
If yearly, I expect Feb 29, 2016 + 1 year => Feb 28, 2017
Thanks.

Try this, if date > 28 use last day of next month else use +1 month
$get_date = strtotime("31-01-2016");
$dt = explode("-",$get_date);
$dt = $dt[0];
var_dump(($dt > 28) ? date("d-m-Y", strtotime("31-01-2016 last day of next month")) : date("d-m-Y", strtotime("31-01-2016 +1 month")));
DEMO

You can call modify with PHP's DateTime object to calculate the next date relative to the current date. The following code shows how you would do it with your specific situation.
$next_bill_date = new DateTime();
switch($plan_interval_unit) {
case "year":
$next_bill_date->modify("last day of next month");
break;
case "month":
$next_bill_date->modify("last day of this month next year");
break;
}

May be something like that:
if (date('d') !== date('d', strtotime('+1 month'))
date ('Y-m-d H:i:s', strtotime('last day of next month'));
if (date('d') !== date('d', strtotime('+1 year'))
date ('Y-m-d H:i:s', strtotime('last day of this month next year'));

You can use PHP inbuilt strtotime() function
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2016-12-06')));

function get_next_billing_date($now, $type, $format = 'Y-m-d')
{
$date = new DateTime($now);
$y = $date->format("Y");
$m = $date->format("m");
$d = $date->format("d");
if ($type == 'year') {
$y++;
if ($m == 2 && $d == 29) {
$d = 28;
}
} else if ($type == 'month') {
if ($m == 12) {
$y++;
$m = 1;
} else {
$m++;
}
$first_date = sprintf("%04d-%02d-01", $y, $m);
$last_day_of_next_month = date('t', strtotime($first_date));
if ($d > $last_day_of_next_month) {
$d = $last_day_of_next_month;
}
} else {
// not supported
return false;
}
$date->setDate($y, $m, $d);
return $date->format($format);
}

Related

How to add date + 6 month but exclude Sundays using PHP

I am creating a scheduler script that if I executed a task today my next task will be 6 months from today (Semi-Annual) but my problem is people has no activity on Sundays so it must be adjusted on Monday instead.
How can I add 6 month to today's date but exclude Sundays, I am using PHP and able to add 6 months using below code so far.
My code is this.
<?php echo date('Y-m-d', strtotime('+6 months'));?>
Here's the answer: https://stackoverflow.com/a/12365635/6557808
Provides more than you're asking for and is pretty clear.
Here is a small function to it. Just pass from and end date
<?php
function number_of_working_days($from, $to) {
$workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...)
$from = new DateTime($from);
$to = new DateTime($to);
$to->modify('+1 day');
$interval = new DateInterval('P1D');
$periods = new DatePeriod($from, $interval, $to);
$days = 0;
foreach ($periods as $period) {
if (!in_array($period->format('N'), $workingDays)) continue;
$days++;
}
return $days;
}
echo number_of_working_days('2017-05-06', '2017-05-08');
?>
Complete Answer is here:--
function daysToAdd($startDate,$endDate)
{
$count=0;
While($startDate!=$endDate){
$lpcnt=0;
$begin = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
if($dt->format( "l" )=='Sunday'){
$count++;
$lpcnt++;
}
}
$day = date('D', strtotime($endDate));
if($day=='Sun'){
$count=$count+1;
$lpcnt=$lpcnt+1;
}
$startDate=date('Y-m-d', strtotime($endDate . ' +1 day'));
$endDate=date('Y-m-d', strtotime($endDate . ' +'.$lpcnt.' day'));
if($startDate!=$endDate){
daysToAdd($startDate,$endDate);
}else{
if(date('D', strtotime($startDate))=='Sun') {
$count=$count+1;
break;
}
}
}
return $count;
}
$currentDate=date('Y-m-d');
$postSixMonth=date('Y-m-d', strtotime("+5 months"));
$daysToadd=daysToAdd($currentDate,$postSixMonth);
$requiredDate=date('Y-m-d', strtotime($postSixMonth . ' +'.$daysToadd.' days'));
echo $requiredDate;exit;
As stated in the PHP.net date() documentation you can check for a specific day number.
So, using that knowledge you can either:
if (date("w", strtotime("+6 months")) == 0) {
echo date("Y-m-d", strtotime("+6 months +1 days"));
}
else {
echo date("Y-m-d", "+6 months");
}
or something in the lines of Bilal Ahmed's answer using a switch
switch (date("w", strtotime("+6 months")) == 0) {
case 0: // Sunday
echo date("Y-m-d", strtotime("+6 months +1 days"));
break;
case 1: // Monday
case 2: // Tuesday
case 3: // Wednesday
case 4: // Thursday
case 5: // Friday
case 6: // Saturday
echo date("Y-m-d", "+6 months");
break;
}
which might be better if there is something to do for every different day as date() will not be executed every time you perform a case. (Note: I did not compare speeds)
EDIT: oneliner based on the comment of blokish on the askers post
echo date("w", strtotime("+6 months")) == 0 ? date("Y-m-d", "+6 months +1 days") : date("Y-m-d", "+6 months");

PHP subtract 1 month from date formatted with date ('m-Y')

I'm trying to subtract 1 month from a date.
$today = date('m-Y');
This gives: 08-2016
How can I subtract a month to get 07-2016?
<?php
echo $newdate = date("m-Y", strtotime("-1 months"));
output
07-2016
Warning! The above-mentioned examples won't work if call them at the end of a month.
<?php
$now = mktime(0, 0, 0, 10, 31, 2017);
echo date("m-Y", $now)."\n";
echo date("m-Y", strtotime("-1 months", $now))."\n";
will output:
10-2017
10-2017
The following example will produce the same result:
$date = new DateTime('2017-10-31 00:00:00');
echo $date->format('m-Y')."\n";
$date->modify('-1 month');
echo $date->format('m-Y')."\n";
Plenty of ways how to solve the issue can be found in another thread: PHP DateTime::modify adding and subtracting months
Try this,
$today = date('m-Y');
$newdate = date('m-Y', strtotime('-1 months', strtotime($today)));
echo $newdate;
Depending on your PHP version you can use DateTime object (introduced in PHP 5.2 if I remember correctly):
<?php
$today = new DateTime(); // This will create a DateTime object with the current date
$today->modify('-1 month');
You can pass another date to the constructor, it does not have to be the current date. More information: http://php.net/manual/en/datetime.modify.php
I used this to prevent the "last days of month"-error. I just use a second strtotime() to set the date to the first day of the month:
<?php
echo $newdate = date("m-Y", strtotime("-1 months", strtotime(date("Y-m")."-01")));
if(date("d") > 28){
$date = date("Y-m", strtotime("-".$loop." months -2 Day"));
} else {
$date = date("Y-m", strtotime("-".$loop." months"));
}
$lastMonth = date('Y-m', strtotime('-1 MONTH'));
First change the date format m-Y to Y-m
$date = $_POST('date'); // Post month
or
$date = date('m-Y'); // currrent month
$date_txt = date_create_from_format('m-Y', $date);
$change_format = date_format($date_txt, 'Y-m');
This code minus 1 month to the given date
$final_date = new DateTime($change_format);
$final_date->modify('-1 month');
$output = $final_date->format('m-Y');
Try this,
$effectiveDate = date('2018-01'); <br>
echo 'Date'.$effectiveDate;<br>
$effectiveDate = date('m-y', strtotime($effectiveDate.'+-1 months'));<br>
echo 'Date'.$effectiveDate;
$currentMonth = date('m', time());
$currentDay = date('d',time());
$currentYear = date('Y',time());
$lastMonth = $currentMonth -1;
$one_month_ago=mkdate(0,0,0,$one_month_ago,$currentDay,$currentYear);
This could be rewritten more elegantly, but it works for me
I realize this is an old post, but I've been solving the same issue, and here is what I came up with to account for all the variability. This function is just trying to get relative dates, so same day of prior month, or last day of month if you are on the last day, regardless of exactly how many days a month has. So goal is given '2010-03-31' and subtract a month, we should output '2010-02-28'.
private function subtractRelativeMonth(DateTime $incomingDate): DateTime
{
$year = $incomingDate->format('Y');
$month = $incomingDate->format('m');
$day = $incomingDate->format('d');
$daysInOldMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if ($month == 1) { //It's January, so we have to go back to December of prior year
$month = 12;
$year--;
} else {
$month--;
}
$daysInNewMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
if ($day > $daysInNewMonth && $month == 2) { //New month is Feb
$day = $daysInNewMonth;
}
if ($day > 29 && $daysInOldMonth > $daysInNewMonth) {
$day = $daysInNewMonth;
}
$adjustedDate = new \DateTime($year . '-' . $month . '-' . $day);
return $adjustedDate;
}

PHP - how to use $timestamp to check if today is Monday or 1st of the month?

I have been looking through examples online, and I am finding them a bit cryptic or overkill.
What I need to do is something like this:
$timestamp = time();
and then find out if the day is a Monday or a fist of the month?
I am sure it is possible, I am just not sure how to do that.
Actually, you don't need timestamp variable because:
Exerpt from date function of php.net:
Returns a string formatted according to the given format string using
the given integer timestamp or the current time if no timestamp is
given. In other words, timestamp is optional and defaults to the value
of time().
if(date('j', $timestamp) === '1')
echo "It is the first day of the month today\n";
if(date('D', $timestamp) === 'Mon')
echo "It is Monday today\n";
This should solve it:
$day = date('D');
$date = date('d')
if($day == Mon){
//Code for monday
}
if($date == 01){
//code for 1st fo the month
}
else{
//not the first, no money for you =/
}
This will grab.. Monday from mysql
$monday = 1; //tuesday= 2.. sunday = 7
AND $monday = (date_format(from_unixtime(your_date_column),'%w'))
OR days..
$day = 1; ///1st in month
AND $day = (date_format(from_unixtime(your_date_column),'%d'))
JUST TO KNOW
$date = date("d"); //1st?
$dayinweek = date("w"); //monday? //as a number in a week what you need more then just "Monday" I guess..
You can use: strtotime
$firstdaymonth = strtotime('first day this month');
Because $date can monday or sunday. Should be check it
public function getWeek($date){
$date_stamp = strtotime(date('Y-m-d', strtotime($date)));
//check date is sunday or monday
$stamp = date('l', $date_stamp);
if($stamp == 'Mon'){
$week_start = $date;
}else{
$week_start = date('Y-m-d', strtotime('Last Monday', $date_stamp));
}
if($stamp == 'Sunday'){
$week_end = $date;
}else{
$week_end = date('Y-m-d', strtotime('Next Sunday', $date_stamp));
}
return array($week_start, $week_end);
}
Since PHP >= 5.1 it is possible to use date('N'), which returns an ISO-8601 numeric representation of the day of the week, where 1 is Monday and 7 is Sunday.
So you can do
if(date('N', $timestamp) === '1' || date('j', $timestamp) === '1')) {
echo "Today it is Monday OR the first of the month";
}

How to get all fridays from current month

I have a problem,
$fridays = array();
$fridays[0] = date('Y-m-d', strtotime('first friday of this month'));
$fridays[1] = date('Y-m-d', strtotime('second friday of this month'));
$fridays[2] = date('Y-m-d', strtotime('third friday of this month'));
$fridays[3] = date('Y-m-d', strtotime('fourth friday of this month'));
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));
but there is no fifth friday. Some months have fifth fridays. How to check and not set the last item array?
$fifth = strtotime('fifth friday of this month');
if (date('m') === date('m', $fifth)) {
$fridays[4] = date('Y-m-d', $fifth);
}
You can do this using the PHP date function. Get the month you want in $timestamp and then do something like this:
<?php
function fridays_get($month, $stop_if_today = true) {
$timestamp_now = time();
for($a = 1; $a < 32; $a++) {
$day = strlen($a) == 1 ? "0".$a : $a;
$timestamp = strtotime($month . "-$day");
$day_code = date("w", $timestamp);
if($timestamp > $timestamp_now)
break;
if($day_code == 5)
#$fridays++;
}
return $fridays;
}
echo fridays_get('2011-02');
You can find a similar post about this: In PHP, how to know how many mondays have passed in this month uptil today?
I have a function to count fridays in a month to my very own application.... it could be helpful for someone.
function countFridays($month,$year){
$ts=strtotime('first friday of '.$year.'-'.$month.'-01');
$ls=strtotime('last day of '.$year.'-'.$month.'-01');
$fridays=array(date('Y-m-d', $ts));
while(($ts=strtotime('+1 week', $ts))<=$ls){
$fridays[]=date('Y-m-d', $ts);
}return $fridays;
}
I'm not on a machine that I could test this but what about something along the lines of....
if(date('Y-m-d', strtotime('fifth friday of this month')) > ""){
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));
}
The link below does exactly the same thing and will cover exactly what you are wanting todo without clunky if statements like above..
VERY Similar reading: read more...
the month will have 5 fridays only if it has 30 days and first friday is 1st or 2nd day of the month or if it has 31 days and the first friday is 1st, 2nd or 3rd day of the month, so you can make conditional statement based on this calculation for 5th friday
for($i=0;$i<=5;$i++)
{
echo date("d/m/y", strtotime('+'.$i.' week friday september 2012'));
}
I used this as the basis for my own solution:
$fridays = array();
$fridays[0] = date('d',strtotime('first fri of this month'));
$fridays[1] = $fridays[0] + 7;
$fridays[2] = $fridays[0] + 14;
$fridays[3] = $fridays[0] + 21;
$fridays['last'] = date('d',strtotime('last fri of this month'));
if($fridays[3] == $fridays['last']){
unset($fridays['last']);
}
else {
$fridays[4] = $fridays['last'];
unset($fridays['last']);
}
print_r($fridays);
I needed to get an array of every Friday in a month, even if there were 5 and this seemed to do the trick using the original question as my basis.
<?php //php 7.0.8
$offDays = array();
$date = date('2020-02');
$day= 'Friday';
$offDays[0] = date('d',strtotime("first {$day} of ".$date));
$offDays[1] = $offDays[0] + 7;
$offDays[2] = $offDays[0] + 14;
$offDays[3] = $offDays[0] + 21;
$offDays['last'] = date('d',strtotime("last {$day} of ".$date));
if($offDays[3] == $offDays['last']){
unset($offDays['last']);
}
else {
$offDays[4] = $offDays['last'];
unset($offDays['last']);
}
foreach($offDays as $off){
echo date('Y-m-d-D',strtotime(date($date."-".$off)));
echo "\n";
}
?>

Dates in php Sunday and Saturday?

i have one date.
example : $date='2011-21-12';
data format :yyyy-dd-mm;
IF date is Saturday or Sunday.
if Saturday add 2 day to the given date.
if Sunday add 1 day to the given date.
?
In a single line of code:
if (date('N', $date) > 5) $nextweekday = date('Y-m-d', strtotime("next Monday", $date));
If the day of week has a value greater than 5 (Monday = 1, Sat is 6 and Sun is 7) set $nextweekday to the YYYY-MM-DD value of the following Monday.
Editing to add, because the date format may not be accepted, you would need to reformat the date first. Add the following lines above my code:
$pieces = explode('-', $date);
$date = $pieces[0].'-'.$pieces[2].'-'.$pieces[1];
This will put the date in Y-m-d order so that strtotime can recognize it.
You can use the date and strtotime functions for this, like so:
$date = strtotime('2011-12-21');
$is_saturday = date('l', $date) == 'Saturday';
$is_sunday = date('l', $date) == 'Sunday';
if($is_saturday) {
$date = strtotime('+2 days', $date);
} else if($is_sunday) {
$date = strtotime('+1 days', $date);
}
echo 'Date is now ' . date('Y-m-d H:i:s', $date);
What about this:
$date='2011-21-12';
if (date("D", strtotime($date)) == "Sat"){
$new_date = date("Y-m-d", strtotime("+ 2 days",$date);
}
else if (date("D", strtotime($date)) == "Sun"){
$new_date = date("Y-m-d", strtotime("+ 1 day",$date);
}
The DateTime object can be really helpful for anything like this.
In this case.
$date = DateTime::createFromFormat('Y-d-m', '2011-21-12');
if ($date->format('l') == 'Sunday')
$date->modify('+1 day');
elseif ($date->format('l') == 'Saturday')
$date->modify('+2 days');
If you want to get the date back in that format.
$date = $date->format('Y-d-m');
$date = '2011-21-12'
$stamp = strtotime($date);
$day = date("l", $stamp);
if ($day == "Saturday"){
$stamp = $stamp + (2*+86400);
}elseif($day == "Sunday"){
$stamp = $stamp + 86400;
}
echo date("Y-d-m", $stamp);
The only reason i can think why this wouldnt work is strtotime not recognising that data format...
For sundays date('w',$date) will return 0, so the simplest test code is:
if(!date('w',strtotime($date)) { ... } //sunday
I just subtracted the difference from 8 an added it to the days.
if(date('N', strtotime($date)) >= 6) {
$n = (8 - date("N",strtotime($date)));
$date = date("Y-m-d", strtotime("+".$n." days");
}

Categories