I have a number and I want to increase the month of date so if the date is 2016-01-01 will return 2016-04-01
$month = 3;
$date = date('Y-m-d', strtotime(str_replace('/', '-', $this->input->post('date'))));
Untested, But you could do something like:
$month = 3;
$newdate = date('Y-m-d', strtotime("+".$month." months", str_replace('/', '-', $this->input->post('date'))));
You can add three months in a given date by using this:
$Date = date('Y-m-d', strtotime("+3 months", strtotime(str_replace('/','-', $this->input->post('date') ))));
Related
I am using bootstrap Datepicker and I want to set the date for current/last Sunday.The code below is showing the content form $date to $date1.
if (!isset($_POST['new-date'])){
$date = date('Y-m-d H:i:s');// I want to set last/current sunday
date here.
$date1 = date("Y-m-d H:i:s", strtotime("$date +6 day"));
$d = explode(" ",$date);
$d1 = explode(" ",$date1);
} else {
$d[0] = $_POST['new-date'];
$d1[0] = date("Y-m-d", strtotime("$d[0] +6 day"));
}
$d[0] = $_POST['new-date'];
The ['new-date'] may be any day/date from the calendar but It should select the last Sunday of that particular week.
Your code seems to be written in PHP, rather than JavaScript, so I'm not sure how relevant the question title or tags are.
// Check if the day of the week is a Sunday. If yes, use today's date. If no, use last Sunday's date
$timestamp = date('N') == 7 ? strtotime('today') : strtotime('last sunday');
$date = date('Y-m-d H:i:s', $timestamp);
die($date);
Edit
You asked in the comments how you can find last Sunday for any given date.
$timestamp = strtotime($_POST['date'] ?? 'today');
$sunday = (date('N', $timestamp) == 7) ? $timestamp : strtotime('last sunday', $timestamp);
$date = date('Y-m-d H:i:s', $sunday);
die($date);
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;
}
I want to separate 4 quarter months from current month.
$q1Start = date('Y-m-01',strtotime('-2 months'));
$q1End = date('Y-m-d');
$q2Start = date('Y-m-01',strtotime('-5 months'));
$q2End = date('Y-m-d',strtotime('-3 months'));
$q3Start = date('Y-m-01',strtotime('-8 months'));
$q3End = date('Y-m-t',strtotime('-6 months'));
$q4Start = date('Y-m-01',strtotime('-11 months'));
$q4End = date('Y-m-t',strtotime('-9 months'));
But its not showing the exact date.
I want like this.
2015-06-01
2015-08-31
2015-03-01
2015-05-31
2015-02-28
2014-12-01
2014-09-01
2014-11-30
How to decrease by current month? Thanks
Since the quarters should be based on the current month, it's easier to start with the first day of the month for calculations.
$dayOfMonth = date('d');
$firstDayOfCurrentMonth = strtotime(sprintf('-%d days + 1 day midnight', $dayOfMonth));
$q1Start = date('Y-m-d', strtotime('-2 months', $firstDayOfCurrentMonth));
$q1End = date('Y-m-d', strtotime('+1 month - 1 day', $firstDayOfCurrentMonth));
$q2Start = date('Y-m-d', strtotime('-5 months', $firstDayOfCurrentMonth));
$q2End = date('Y-m-d', strtotime('-2 month - 1 day', $firstDayOfCurrentMonth));
$q3Start = date('Y-m-d', strtotime('-8 months', $firstDayOfCurrentMonth));
$q3End = date('Y-m-d', strtotime('-5 month - 1 day', $firstDayOfCurrentMonth));
$q4Start = date('Y-m-d', strtotime('-11 months', $firstDayOfCurrentMonth));
$q4End = date('Y-m-d', strtotime('-8 month - 1 day', $firstDayOfCurrentMonth));
This sets all variables to the expected values.
I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time() variable works, but I am not sure how to +1 month onto that?
Any ideas, suggestions. Cheers!
Try this
$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));
or use DateTime()
$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
Use this:
Current Date:
echo "Today is " . date("Y/m/d");
1 Month to the Current Date:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month"); // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); // Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose
?>
shorter : $today=date("Y-m-d"); $date=
This one liner worked for me:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
The given answers may not give you the results you might expect or desire.
Consider:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
You could also consider using the Carbon package.
The solution would look like this:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();
Here is the link for reference https://carbon.nesbot.com/docs/
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");
}