I am trying to fetch start date and end date of "2nd week February 2020":
$week = "02";
$month = "02";
$year = "2020";
$startdateofweek = date('d-m-Y', strtotime('monday second week of February 2020'));
$enddateofweek = date('d-m-Y', strtotime('saturday second week of February 2020'));
I want to output as :
array("start_week_date"=>"03-02-2020","end_week_date"=>"09-02-2020")
$sunday = strtotime('2 sunday February 2020');
$startdateofweek = date('d-m-Y', strtotime('last monday', $sunday));
$enddateofweek = date('d-m-Y', $sunday);
I have three columns in my table, future 3 months like fistdate(next month), secondate(+2 next month) and thirdate(+3 next month) which are DATE types. I want to save my dates as follows 09/09/2017, 09/10/2017 and 09/11/2017 and current month is 07/08/2017 and above 3 dates are not related to current month date like 07/08/2017
I have tried following code but it is related to current month
$date1 = date('Y-m-d', strtotime('+1 month'));
$date2 = date('Y-m-d', strtotime('+2 month'));
$date3 = date('Y-m-d', strtotime('+3 month'));
$install = array(
"invoiceid" =>$this->input->post('salesInvoiceno', TRUE),
"fistdate" =>$date1,
"secondate" =>$date2,
"thirdate" =>$date3
);
$time = strtotime("2010-08-06");$date1 = date('Y-m-d', strtotime('+1 month',$time));$date2 = date('Y-m-d', strtotime('+2 month',$time));$date3 = date('Y-m-d', strtotime('+3 month',$time));echo $date1.' '.$date2.' '.$date3;
How to find get week duration or week range from monday to friday for the given date in php.
for example i have date
$date = "2013-02-24";
Now i want range of date from monday to friday for the month
Just Try Out this answer.
$date = "2013-02-24";
$week = date('W', strtotime($date));
$year = date('o', strtotime($date));
echo "first day of week". $from = date("Y-m-d", strtotime("{$year}-W{$week}-1"));
echo "end day of week". $to = date("Y-m-d", strtotime("{$year}-W{$week}-5"));
function week_range($date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last monday', $ts);
return array(date('Y-m-d', $start),
date('Y-m-d', strtotime('next friday', $start)));
}
And call it like this:
list($start_date, $end_date) = week_range('2009-05-10');
how to get the previous 3 months in php ex(If i say DEC.. It should display the previous 3 months i.e., OCT NOV DEC)
You can use the strtotime function like this:
echo date('M', strtotime('-3 month'));
So you specify previous dates with minus sign.
echo date('M', strtotime('0 month'));
echo date('M', strtotime('-1 month'));
echo date('M', strtotime('-2 month'));
echo date('M', strtotime('-3 month'));
Results:
Dec
Nov
Oct
Sep
You can do the same if you are using a loop like this:
for ($i = -3; $i <= 0; $i++){
echo date('M', strtotime("$i month"));
}
Results:
Sep
Oct
Nov
Dec
Check out the documentation too see many other friendly date and time keywords strtotime supports:
http://php.net/manual/en/function.strtotime.php
Voted answer is almost correct.
Correct solution is:
for ($i = -3; $i <= 0; $i++){
echo date('M', strtotime("$i month", strtotime(date("Y-m-15"))));
}
Explain: strtotime default implementation is:
date ( string $format [, int $timestamp = time() ] ) : string
As you can see there is timestamp with default value of: time().
Last say today is 31th March.
Because there is no 31th Feb
echo date('M', strtotime("-1 month"));
will return March
On the other hand:
echo date('M', strtotime("+1 month"));
will return May (April will be skipped).
Because of that issue we have to setup timestamp value which is a safe date.
Every date between 1-28 is safe because every month have got that date. In my example I had choose 15th day of month.
$month = date('M');
$year = date('Y');
$no_of_mnths = date('n',strtotime("-2 months"));
$remaining_months = (12 - $no_of_mnths)."\r\n";
$tot = $remaining_months+1;
for($j=0;$j<$tot;$j++){
echo date('M',strtotime(''.$no_of_mnths.' months'))
$no_of_mnths++;
}
This may help you`
<?php
$date = explode("-", "2016-08-31");
if($date[2]== "01"){$days = "-0 days";}else{$days = "-1 days";}
$time = strtotime($days, mktime(0, 0, 0, $date[1], $date[2], $date[0]));
$MTD = date('Y-m-01', strtotime('0 month'));
$M1 = date('Y-m-01', strtotime('-1 month'));
$M2 = date('Y-m-01', strtotime('-2 month'));
$M3 = date('Y-m-01', strtotime('-3 month'));
$rng = array();
$rng[$MTD] = strftime("%B, %Y", strtotime(" ", $time));
$rng[$M1] = strftime("%B, %Y", strtotime("first day of previous month", $time));
$rng[$M2] = strftime("%B, %Y", strtotime("-2 months", $time));
$rng[$M3] = strftime("%B, %Y", strtotime("-3 months", $time));
?>
Here $MTD ,$M1,$M2,$M3 will give date in the form of "dd-mm-yyyy" and $rng[$MTD], $rng[$M1],$rng[$M2],$rng[$M3] give date in the form of"Name of Month,year" => (i.e August,2016)
I have two columns in my db: start_date and end_date, which are both DATE types. My code is updating the dates as follows:
$today_date = date("Y-m-d");
$end_date = date("Y-m-d"); // date +1 month ??
$sql1 = "UPDATE `users` SET `start_date` = '".$today_date."', `end_date` = '".$end_date."' WHERE `users`.`id` ='".$id."' LIMIT 1 ;";
What is the best way to make $end_date equal to $start_date + one month? For example, 2000-10-01 would become 2000-11-01.
You can use PHP's 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('2015-01-01')));
Just note that +1 month is not always calculated intuitively. It appears to always add the number of days that exist in the current month.
Current Date | +1 month
-----------------------------------------------------
2015-01-01 | 2015-02-01 (+31 days)
2015-01-15 | 2015-02-15 (+31 days)
2015-01-30 | 2015-03-02 (+31 days, skips Feb)
2015-01-31 | 2015-03-03 (+31 days, skips Feb)
2015-02-15 | 2015-03-15 (+28 days)
2015-03-31 | 2015-05-01 (+31 days, skips April)
2015-12-31 | 2016-01-31 (+31 days)
Some other date/time intervals that you can use:
$date = date('Y-m-d'); // Initial date string to use in calculation
$date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+2 week', strtotime($date)));
$date = date('Y-m-d', strtotime('+1 month', strtotime($date)));
$date = date('Y-m-d', strtotime('+30 days', strtotime($date)));
The accepted answer works only if you want exactly 31 days later. That means if you are using the date "2013-05-31" that you expect to not be in June which is not what I wanted.
If you want to have the next month, I suggest you to use the current year and month but keep using the 1st.
$date = date("Y-m-01");
$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;
This way, you will be able to get the month and year of the next month without having a month skipped.
You do not actually need PHP functions to achieve this. You can already do simple date manipulations directly in SQL, for example:
$sql1 = "
UPDATE `users` SET
`start_date` = CURDATE(),
`end_date` = DATE_ADD(CURDATE(), INTERVAL 1 MONTH)
WHERE `users`.`id` = '" . $id . "';
";
Refer to http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime
If you want a specific date in next month, you can do this:
// Calculate the timestamp
$expire = strtotime('first day of +1 month');
// Format the timestamp as a string
echo date('m/d/Y', $expire);
Note that this actually works more reliably where +1 month can be confusing. For example...
Current Day | first day of +1 month | +1 month
---------------------------------------------------------------------------
2015-01-01 | 2015-02-01 | 2015-02-01
2015-01-30 | 2015-02-01 | 2015-03-02 (skips Feb)
2015-01-31 | 2015-02-01 | 2015-03-03 (skips Feb)
2015-03-31 | 2015-04-01 | 2015-05-01 (skips April)
2015-12-31 | 2016-01-01 | 2016-01-31
Adding my solution here, as this is the thread that comes in google search. This is to get the next date of month, fixing any skips, keeping the next date within next month.
PHP adds current months total number of days to current date, if you do +1 month for example.
So applying +1 month to 30-01-2016 will return 02-03-2016. (Adding 31 days)
For my case, I needed to get 28-02-2016, so as to keep it within next month. In such cases you can use the solution below.
This code will identify if the given date's day is greater than next month's total days. If so It will subtract the days smartly and return the date within the month range.
Do note the return value is in timestamp format.
function getExactDateAfterMonths($timestamp, $months){
$day_current_date = date('d', $timestamp);
$first_date_of_current_month = date('01-m-Y', $timestamp);
// 't' gives last day of month, which is equal to no of days
$days_in_next_month = date('t', strtotime("+".$months." months", strtotime($first_date_of_current_month)));
$days_to_substract = 0;
if($day_current_date > $days_in_next_month)
$days_to_substract = $day_current_date - $days_in_next_month;
$php_date_after_months = strtotime("+".$months." months", $timestamp);
$exact_date_after_months = strtotime("-".$days_to_substract." days", $php_date_after_months);
return $exact_date_after_months;
}
getExactDateAfterMonths(strtotime('30-01-2016'), 1);
// $php_date_after_months => 02-03-2016
// $exact_date_after_months => 28-02-2016
You can use strtotime() as in Gazler's example, which is great for this case.
If you need more complicated control use mktime().
$end_date = mktime(date("H"), date("i"), date("s"), date("n") + 1, date("j"), date("Y"));
date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))
date_trunc('month', now()) + interval '1 month'
date_trunc('month', now()) returns start date of month and + interval '1 month' add a month to date.
This function returns any correct number of months positively or negatively. Found in the comment section here:
function addMonthsToTime($numMonths = 1, $timeStamp = null){
$timeStamp === null and $timeStamp = time();//Default to the present
$newMonthNumDays = date('d',strtotime('last day of '.$numMonths.' months', $timeStamp));//Number of days in the new month
$currentDayOfMonth = date('d',$timeStamp);
if($currentDayOfMonth > $newMonthNumDays){
$newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days '.$numMonths.' months', $timeStamp);
} else {
$newTimeStamp = strtotime($numMonths.' months', $timeStamp);
}
return $newTimeStamp;
}
01-Feb-2014
$date = mktime( 0, 0, 0, 2, 1, 2014 );
echo strftime( '%d %B %Y', strtotime( '+1 month', $date ) );
I think this is similar to kouton's answer, but this one just takes in a timeStamp and returns a timestamp SOMEWHERE in the next month. You could use date("m", $nextMonthDateN) from there.
function nextMonthTimeStamp($curDateN){
$nextMonthDateN = $curDateN;
while( date("m", $nextMonthDateN) == date("m", $curDateN) ){
$nextMonthDateN += 60*60*24*27;
}
return $nextMonthDateN; //or return date("m", $nextMonthDateN);
}
I know - sort of late. But I was working at the same problem. If a client buys a month of service, he/she expects to end it a month later. Here's how I solved it:
$now = time();
$day = date('j',$now);
$year = date('o',$now);
$month = date('n',$now);
$hour = date('G');
$minute = date('i');
$month += $count;
if ($month > 12) {
$month -= 12;
$year++;
}
$work = strtotime($year . "-" . $month . "-01");
$avail = date('t',$work);
if ($day > $avail)
$day = $avail;
$stamp = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);
This will calculate the exact day n*count months from now (where count <= 12). If the service started March 31, 2019 and runs for 11 months, it will end on Feb 29, 2020. If it runs for just one month, the end date is Apr 30, 2019.
$nextm = date('m', strtotime('+1 month', strtotime(date('Y-m-01'))));
one way of doing this is- first getting last date of current month and then adding 1 day to it, to get next month's first date. This will prevent us from unintentional skipping of months which occurs while using '+1 month'.
$today_date = date("Y-m-d");
$last_date_of_month=date('Y-m-t',strtotime($today_date);//get last date of current month
$last_day_of_month_proper =strtotime($last_day_of_month);
$new_date=date('Y-m-d', strtotime('+1 day',$last_day_of_month_proper));
echo $new_date;