'Round' up a date to the beginning of the next month - php

I need to take an existing date from a variable and display a date that is always the 1st day (01) of whatever the next month is (and accounting for the year as well).
So if I have this:
$date = '2017-03-17'; // YYYY-MM-DD
I need to take that date and make it output this:
2017-04-01 // The first day of the next month
Just another example...
$date = '2017-12-23'; // YYYY-MM-DD
...should be converted to...
2018-01-01 // The first day of the next month

You can use DateTime like:
$dateTime = new DateTime('2017-03-17');
$dateTime->modify('first day of next month');
echo $dateTime->format('Y-m-d');

Simply increment the date by 1 month and set the date to 1st of the month. Do -
date('Y-m-01', strtotime('+1 MONTH', strtotime($date)));
Working code

you can get it using
$date = '2017-03-17';
echo date('Y-m-01', strtotime('+1 month',strtotime($date)));
https://eval.in/790237

Related

How to get last Monday from a specific date

I am trying to get the date of Monday from a specific date.
I try to use
strtotime("last monday")
But it's giving me current last Monday date not from a specific date.
Like I want to know the last Monday date on 2020-10-11 which is 2020-10-05
You can use the class DateTime to create an object at the desired date, and then modify it :
$date = new DateTime('2020-10-11');
$date->modify('last monday');
echo $date->format('Y-m-d'); // output is 2020-10-05
As pointed jspit in comments, if the current date is monday and then if this date should be returned, a simple check can be added to avoid returning the wrong date :
$date = new DateTime('2020-10-11');
if ($date->format('N') != 1) // If the date isn't already monday
$date->modify('last monday');
echo $date->format('Y-m-d'); // output is 2020-10-05

Get first day date of the week on a given date PHP

I have a date 2015-12-16
i want to get the date of the first day's date for the current week of my date
here 2015-12-16 is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14 here)
how could i do it ?
thank you
EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)
You can do the following:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example:
http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
This gets start of this week if you count monday to sunday.
Try this:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));

php add x weeks to a date and then find the next given day

I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.

Set up end of this week, next of this month and end of next month dates

I'm trying to set up a few variables to then pass into my code to add some classes. I know how to do most of it, but it's the setting of the variables that's difficult.
I need to set up three variables that will be compared with the current date.
<?php $current_date = date('Ymd'); ?>
The three variables are: this week, this month and next month.
I had originally set my this week variable as so:
<?php $current_date_week = date('Ymd', strtotime($current_date . ' +7 days')); ?>
But I'm getting this wrong. It shouldn't be from today's date + 7, it should get the date of the end of the current week using the Ymd date format. So if today's date is 20140326 then my $current_date_week should bring back 20140330 and so on.
This month should do the same, get the last day of the current month in Ymd format and next month should get the last day of the next month.
I hope this made sense.
$current_date_week
$current_date_month
$current_date_month_next
Thanks,
R
Do this way:
$current_date_week = date('Ymd', strtotime('this Sunday')); //20140330
$current_date_month = date('Ymd', strtotime('last day of this month'));//20140331
$next_date_month = date('Ymd', strtotime('last day of next month'));//20140430
check available list for formats.
Use these as second arguments to date():
mktime(1,2,3,date('m'),date('d')+7-date('N'),date('Y'));
mktime(1,2,3,date('m')+1,0,date('Y'));
mktime(1,2,3,date('m')+2,0,date('Y'));

Date offset in PHP

I am doing my own calendar. For changing to the next and previous months, I use this function:
date('m',"-1 months");
date('m',"+1 months");
but, when I go to the next month, I can't use this again because -1 and +1 are always taken from now().
Assuming that I can't use dynamic numbers to that offset, I mean
date('m',"$x months");
how can I add or subtract 1 month to a specific date?
For example this date...
$date_today = strtotime($_GET['date']);
$next_month = $date_today +/- 1 month ?!?!??!
$next_month = strtotime('+1 month',$date_today);
Use strtotime...
$next_month = strtotime("+1 month");
Will give you a unix timestamp which you can pass to date...
echo date("m", $next_month);
I currently run a calendar for multiple sites with the same issue. I end up storing the current viewable month as a $_SESSION variable or pass it as a $_POST object when someone clicks on next or prev month.
When you would call it when the next or prev was hit a second time would (or the first time) would be something like
if(!isset($_SESSION['viewablemonth']) && $_SESSION['viewablemonth'] = '') {
$_SESSION['viewablemonth'] = date("m.d.Y");
}
End then do your month addition or subtraction:
$_SESSION['viewablemonth'] = strtotime("+1 month", $_SESSION['viewablemonth']);
You can mix strtotimes up with a reference date so:
$next_month=strtotime("+1 month"); // assumes today
$following_month=strtotime("+1 month", $next_month);
http://uk.php.net/manual/en/function.strtotime.php
Takes two parameters and assumes second parameter is time() current timestamp if ommitted

Categories