get next immediate same date using php - php

i need to get immediate date like if i select date is 05 then output will be 2017-07-05 cause select date already passed
if i select 12 then output will be 2017-06-12 cause this date future date
final if i select previous date of current month then output will be next month same date and if i select future date of current month then output will be same month
i have tired but not working this
$today = date("Y-m-d");
$next_payment_date = date('Y-m-d', strtotime('+1 month', $today));
or
$time = time();
date("Y-m-d", mktime(0,0,0,date("n", $time),date("j",$time)- 1 ,date("Y", $time)));
thanks in your advance

One more option:
https://3v4l.org/Me2Kh
$input = 12;
$day = date("d");
if ($input > $day){
$date = date("Y-m-"). str_pad($input,2,"0", STR_PAD_LEFT);
}else{
$date = date("Y-m-",strtotime("+1 month")). str_pad($input,2,"0", STR_PAD_LEFT);
}
echo $date;
I use str_pad to keep two digit day number.

Try this -
<?php
$day = '05';
$today = date('Y-m-d');
$supplied = date('Y-m-'.$day);
if($today>$supplied){
$final = date('Y-m-d', strtotime("+1 months", strtotime($supplied)));
}
else{
$final = $supplied;
}
echo $today;
echo '<br />';
echo $supplied;
echo '<br />';
echo $final;
What I'm doing here -
Comparing the current and supplied date
Based on comparison, if supplied date is smaller, I'm adding 1 month else dislpaying the supplied date.

use this,
$today = date('Y-m-d');
$nextDate = date('Y-m-d', strtotime('+1 month'));
or $nextDate = date('Y-m-d', strtotime('+1 month', strtotime($today));

I think this may help.
I would consider using DateTime and it's add method for a DateInterval.
$date = new \DateTime('now', new \DateTimeZone('America/New_York'));
$interval = new \DateInterval('P1M');
$date->add($interval);
Here are the supported DateTimeZone values. Make sure to set that to the applicable time zone.
Edit:
DateTime is mutable, so please keep that in mind.

Try this code :
<?php
$selected_date = '2017-06-05';
$current = date('Y-m-d');
//echo $current;
if($selected_date < $current)
{
$newDate = date('Y-m-d',strtotime($selected_date."+1 month"));
echo $newDate; // gives 2017-07-05
}else if($selected_date > $current)
{
$newDate = $current;
echo $newDate; // gives 2017-06-07
}
?>

From what you described in the points at beginning of the question, you could achieve it this way:
$selectedDate = new DateTime('2016-06-05 00:00:00');
$now = new DateTime('now');
$now->setTime(0, 0, 0);
if ($selectedDate < $now) { // Selected date is in past
// Set month and year to current
$selectedDate->setDate(
date('Y'),
date('m'),
$selectedDate->format('d')
);
// Add 1 month
$selectedDate->add(new DateInterval('P1M'));
}
// If selected date is current or in future we do nothing
echo $selectedDate->format('Y-m-d');
For input 2017-06-05 it will return 2017-07-05 as expected, and for current or future date will return the date that was selected. Works also for any past date like 2016-04-05

Related

strtotime('-1 month') returning wrong date if month have 31 days

I'm trying make a function to return the exact date of previous months.
That is a example of my code:
// Dates in TimeStamp
$ts_now = strtotime('now');
$ts_month1 = strtotime("-1 month", $ts_now);
$ts_month2 = strtotime("-2 month", $ts_now);
$ts_month3 = strtotime("-3 month", $ts_now);
// Dates Formated
$date_now = date('Y-m-d', $ts_now);
$date_month1 = date('Y-m-d', $ts_month1);
$date_month2 = date('Y-m-d', $ts_month2);
$date_month3 = date('Y-m-d', $ts_month3);
//Output
echo $date_now; //2020-04-30
echo $date_month1; //2020-03-30
echo $date_month2; //2020-03-01
echo $date_month3; //2020-01-30
The problem is in $date_month2 that represents February, the output is 2020-03-01 instead 2020-02-29 and I suppose that problem will happen in months who have 30 days when present date have 31 days.
What is the best way to resolve that?
As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead.
function getMonthsAgo(int $n): string {
$date = new DateTime();
$day = $date->format('j');
$date->modify('first day of this month')->modify('-' . $n . ' months');
if ($day > $date->format('t')) {
$day = $date->format('t');
}
$date->setDate($date->format('Y'), $date->format('m'), $day);
return $date->format('Y-m-d');
}
// Dates Formated
$date_now = date('Y-m-d');
$date_month1 = getMonthsAgo(1);
$date_month2 = getMonthsAgo(2);
$date_month3 = getMonthsAgo(3);
//Output
echo $date_now;
echo $date_month1;
echo $date_month2;
echo $date_month3;
Output:
2020-04-30
2020-03-30
2020-02-29
2020-01-30

Find date of after 2 months using php

When i have current date is in "31/12/2017". I need to find date of after 2 months that means its February. When its february i need to get as "29/2/2018". But When we use below code i got "03/03/2018". Can you please help me to solve this task,
Here i added my PHP code,
$xmasDay = new DateTime('2017-12-31 + 2 month');
echo $xmasDay->format('Y-m-d');
please try this
<?php
function add_month($date_str, $months)
{
$date = new DateTime($date_str);
// We extract the day of the month as $start_day
$start_day = $date->format('j');
// We add 1 month to the given date
$date->modify("+{$months} month");
// We extract the day of the month again so we can compare
$end_day = $date->format('j');
if ($start_day != $end_day)
{
// The day of the month isn't the same anymore, so we correct the date
$date->modify('last day of last month');
}
return $date->format('Y-m-d');
}
$result = add_month('2017-12-31', 2);
echo $result
calculate from first of the month, then use date t
$orginal = '2017-12-31';
$orginal = explode('-', $orginal);
$originalDate = strtotime($orginal[0] . '-' . $orginal[1] . '-01 00:00:00');
$newDate = strtotime('+2 month', $originalDate);
echo date('Y-m-t', $newDate);
// or
$date = new DateTime();
$date->setTimestamp($newDate);
echo $date->format('Y-m-t');

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;
}

How can I get the weekly dates between start date and end date

I want to get weekly dates on the basis of the start date and end date.
Suppose my start date is '2015-09-08' and end date is '2015-10-08'.
On the basis of these dates I want the following result using PHP. I want the weekly dates between the start date and end date.
2015-09-15
2015-09-22
2015-09-29
2015-10-06
you can take timestamps of both start date and end date and keep adding one weeks's timestamp to the current date until it is less than the end date timestamp.
something like below. check if this is what u asked for
$st=strtotime("2015-09-08");
$ed=strtotime("2015-10-08");
$wk=$st;
while($wk<$ed){
$wk = strtotime('+1 Week',$wk);
if($wk<$ed)
echo date("Y-m-d",$wk);
echo '<br>';
}
Try using:
select *
from table_name
where Column_name > '2015-09-08'
and Column_name < '2009-10-08'
OR
SELECT *
FROM Table_name
WHERE Column_name BETWEEN ‘2015-09-08’ AND ‘2015-10-08’
As you want weekly dates using php. The following code will do for you
<?php
$startdate='2015-09-08';
$enddate='2015-10-08';
$date=$startdate;
while($date<=$enddate)
{
$date = strtotime("+7 day", strtotime($date));
$date=date("Y-m-d", $date);
if($date<=$enddate)
echo $date."<br>";
}
?>
Php s strtotime function might come is handy here. You could try this:
$start = '2015-09-08';
$end = // you end date as string
$offset = strtotime($start);
$limit = strtotime($end);
for($t = $offset; $t < $limit; $t += 86400 * 7){
echo date('Y m d') ;
}
This link here has code to do exactly what you want, you can get the date of every Sunday or Monday or whatever day you choose between two dates
try this:
<?php
date_default_timezone_set('Asia/Kolkata');
$startdate= strtotime("15-09-08");
//$startdate= strtotime("08 September 2015");
$enddate= strtotime("15-10-08");
//$enddate= strtotime("08 October 2015");
$jump_date= $startdate;
if($enddate>$startdate)
while($jump_date< $enddate)
{
$jump_date= strtotime("+1 week", $jump_date);
if($jump_date< $enddate)
echo date('Y-m-d', $jump_date).'<br>';
}
?>
Using the built in php function strtotime to add a period of 1week
$ds='2015-09-08';
$df='2015-10-08';
$ts=strtotime( $ds );
$tf=strtotime( $df );
while( $ts <= $tf ){
$ts = strtotime('+1 week', $ts );
echo date( 'Y-m-d', $ts ).'<br />';
}
<?php
// Set timezone
//date_default_timezone_set('UTC');
// Start date
$date = '2015-09-08';
// End date
$end_date = '2015-10-08';
while (strtotime($date) <= strtotime($end_date)) {
echo $date."<br/>";
$date = date ("Y-m-d", strtotime("+7 day", strtotime($date)));
}
?>

Issues with Adding 1 month to a Unix time stamp

For some reason, I cannot get strtotime('+1 month) to work. Here is my code;
$Date = $_REQUEST['date']; //This is a unix time stamp
$Start = $_REQUEST['start']; //This is a unix time stamp
$End = $_REQUEST['end']; //This is a unix time stamp
to add a month onto my dates;
$monStart =strtotime('+1 month', $Start);
$monEnd =strtotime('+1 month', $End);
$monDate =strtotime('+1 month', $Date);
then to show my changed dates;
$vEnd = date('m/d/Y', $monEnd);
$vStart = date('m/d/Y', $monStart);
$vDate = date('m/d/Y', $monDate);
The problem that I have is that the supplied dates;
$Date = 1/31/2013
$Start = 1/01/2013
$End = 1/31/2013
Return;
$vDate = 3/03/2013
$vStart = 2/01/2013 //Only correct one
$vEnd = 3/03/2013
Please can someone help me?
It's jumping to March because today is 31sth Jan, and adding a month gives 31st Feb, which doesn't exist, so it's moving to the next valid date. This is a PHP bug. You can get more info on that at https://bugs.php.net/bug.php?id=44073
You can try with DateTime to over come this scenario. You can use this function for your requirement
function add_month($date_value, $months, $format = 'm/d/Y') {
$date = new DateTime($date_value);
$start_day = $date->format('j');
$date->modify("+{$months} month");
$end_day = $date->format('j');
if ($start_day != $end_day)
$date->modify('last day of last month');
return $date->format($format);
}
Now you can call :
$vEnd = add_month($monEnd, 1);
$vStart = add_month($monStart, 1);
$vDate = add_month($monDate, 1);
This will give you :
$vDate = '02/28/2013';
$vStart = '02/01/2013';
$vEnd = '02/28/2013';
Hope this helps you :)
DateTime is much better for handling date math as it account for things like days in the month:
$dt = new DateTime('2013-02-01');
$dt->modify('+1 month');
echo $dt->format('Y-m-d');
See it in action
Since you're using timestamps it might look like this:
$dt = new DateTime('#'.$_REQUEST['start']);
$dt->modify('+1 month');
echo $dt->format('m/d/Y');

Categories