How can i get 2nd monday after the input date?
i have solution but that not feasible
$date = new DateTime();
$date->modify('next monday');
$next_monday = $date->format('Y-m-d');
$date = new DateTime($next_monday);
$date->modify('next monday');
$next_monday = $date->format('Y-m-d');
Please suggest me some method to do so.
Your DateTime object's modify method takes the same type of arguments that strtotime does. You're already using 'next monday', you just need to use 'second monday' instead.
$date->modify('second monday');
echo $date->format('Y-m-d');
Also, in case you didn't know this, some of the DateTime methods can be chained:
echo $date->modify('second monday')->format('Y-m-d');
You can do it with strtotime() but if you think it is too costly, you can use date and DateInterval as well.
$date = new DateTime('2017-02-15 13:03:00');
// move back to past Monday
$num = (date("w", $date->getTimestamp())) - 1;
$offset = new DateInterval("P{$num}D");
$offset->invert = 1;
// move forward two weeks
$interval = new DateInterval('P2W');
$next_second_monday = $date->add($offset)->add($interval);
And $next_second_monday will be:
DateTime Object
(
[date] => 2017-02-27 13:03:00.000000
[timezone_type] => 3
[timezone] => UTC
)
There is probably another way to do so but using MomentPHP You could get the start of today's week, adding 1 day ( to get to monday ) and then adding two week, you would get to the second next monday.Something like that :
<?php
$m = \Moment\moment();
$sunday = $m->startOf('week');
$monday = $sunday->addDays(1);
$2nextMonday = $monday->addWeeks(2);
Related
I'm using Carbon for the dates. Let's take 1st of March for example. By default Carbon returns the 1st of March for the current year.
//Returns current year value
$date = Carbon::parse('first day of March');
Is it possible to get it definitely from the future (next year), if it has already passed in this year, without using if conditions.
//Shorten this part
$date = Carbon::parse('first day of March');
if ($date->lessThanOrEqualTo(Carbon::now())) {
$date->addYear();
}
$output = $date->format('d-m-Y');
Carbon is just a wrapper class for PHP's DateTime class.
Therefore, this should work:
<?php
$date = new DateTime('2018-03-01');
$today = new DateTime();
if ($date < $today) {
$date->modify('+1 year');
}
echo $date->format('d/m/Y');
Output: 01/03/2019
See it working here https://3v4l.org/i1HvL
Learn the actual PHP DateTime class here https://secure.php.net/manual/en/class.datetime.php
I have no idea why you wouldn't use if to check IF something. Maybe you'd like a ternary better?
$date = ($date < $today) ? $date->modify('+1 year') : $date;
Maybe not cleaner than your method, but it's different and it's working.
I create an array with all the dates relevant, today, 1 of March, and 1 of March next year and sort them.
Then I find 'today' with array_search in the array and grab the next value.
$date = array(Carbon::parse('today'),Carbon::parse('first day of March'), Carbon::parse('first day of March + 1 year'));
sort($date);
$date = $date[array_search(Carbon::parse('today'), $date)+1];
var_dump($date);
I honestly think this is slower than a if(), but it does not use an if() as OP requested :-)
I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!
See strtotime()
strtotime('next tuesday');
You could probably find out if you have gone past that day by looking at the week number:
$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);
if ($weekNoNextTuesday != $weekNo) {
//past tuesday
}
I know it's a bit of a late answer but I would like to add my answer for future references.
// Create a new DateTime object
$date = new DateTime();
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
The nice thing is that you can also do this with dates other than today:
// Create a new DateTime object
$date = new DateTime('2006-05-20');
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
To make the range:
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
References
PHP Manual - DateTime
PHP Manual - DateInterval
PHP Manual - DatePeriod
PHP Manual - clone
The question is tagged "php" so as Tom said, the way to do that would look like this:
date('Y-m-d', strtotime('next tuesday'));
For some reason, strtotime('next friday') display the Friday date of the current week. Try this instead:
//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14
$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07
You can use Carbon library.
Example: Next week friday
Carbon::parse("friday next week");
PHP 7.1:
$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();
PHP manual getTimestamp()
Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:
Module Module1
Sub Main()
Dim d As Date = Now
Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
Console.WriteLine("next friday is " & nextFriday)
Console.ReadLine()
End Sub
End Module
If I understand you correctly, you want the dates of the next 7 days?
You could do the following:
for ($i = 0; $i < 7; $i++)
echo date('d/m/y', time() + 86400 * $i);
Check the documentation for the date function for the format you want it in.
if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.
var date=new Date();
getNextDayOfWeek(date, 2);
// this is for finding next tuesday
function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
Hope this will be helpfull to you, thank you
The PHP documentation for time() shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.
Suppose ,i have three variables .
$curr_day = "monday";
$curr_date = "12/12/2222";//('d/m/y')
$next_day = "friday";
$next_date = ??;
what will be the next date ?
Is there any "php functions" that helps me to find out $next_date ?
Thanks In Advance .
You only need:
var_dump(new DateTime('2016-05-29 next friday'));
will give you next friday after "2016-05-29".
object(DateTime)[2]
public 'date' => string '2016-06-03 00:00:00.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
You complete example with the function you need:
$curr_day = "monday";
$curr_date = "26/05/2016";
$date = DateTime::createFromFormat('d/m/Y', $curr_date);
$nextDate = new DateTime($date->format('Y-m-d').' next friday');
var_dump($nextDate->format("d/m/Y"));
string '27/05/2016' (length=10)
did you try :
var_dump(new DateTime('next friday'));
I usually use DateTime to solve date related problems. Take a look at the following example:
// Make a new DateTime object using your date
$date = DateTime::createFromFormat('Y-m-d', '2016-05-25');
// Modify the object to get the next date
$date->modify('next friday');
// Output the date
echo $date->format('Y-m-d');
I'm assuming your $curr_date is in m/d/Y format (if it's not, please clarify your question because 12/12 it's confusing).
You could use strtotime as an easy way to calculate relative dates. See Example #1.
$curr_day = "Monday";
$curr_date = "12/12/2222";
$next_day = "Friday";
// Convert your string $curr_date to timestamp
$curr_date = strtotime( $curr_date );
// Then use again strtotime to calculate the relative date.
echo date('m-d-Y', strtotime( "next " . $next_day, strtotime( $curr_date ) ) );
Hope this helps!
PS: Btw, 12/12/2222 is not Monday but Thursday.
TRY! this...
// get next date
$d = new DateTime($curr_date);
$next_date = new DateTime($d->format('d/m/y'));
echo $next_date->modify('+1 day'); //13/12/2222
Whats the best method to find the date in date('m.d.Y') format that is 1 day ago from another date in date('m.d.Y') format in PHP? Must be applicable to v5.1.6.
I need to build a recursive method to iterate through previous days.
Thanks!
------------Revision -------
$date = date('m.d.Y');
$date = date('m.d.Y', strtotime($date .' -1 day'));
The doesn't seem to work :(
The date must begin and end in the same format 'm.d.Y'
You really should be looking at using DateTime, DateInterval, and DatePeriod classes for this. As an example:
$start_date = '12.31.2013'; // your input date
$start_date_time = DateTime::createFromFormat('m.d.Y', $start_date);
$one_day_interval = new DateInterval('P1D');
// iterate X number of days example
$iteration_days = 30; // some value for number of iterations you want
$count_period = new DatePeriod($start_date_time, $one_day_interval, $iteration_days);
foreach($count_period as $day) {
// do something
}
// iterate between start and end date example
$end_date = '3.31.2014';
$end_date_time = DateTime::createFromFormat('m.d.Y', $end_date);
$date_period = new DatePeriod($start_date_time, $one_day_interval, $end_date_time);
foreach($date_period as $day) {
// do something
}
$date = DateTime::createFromFormat('m.d.Y', $yourDate);
$date->sub(new DateInterval('P1D');
try like this:
$date =date("Y-m-d"); //set your date
echo date('m.d.Y', strtotime($date .' -1 day'));
demo : https://eval.in/107144
I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!
See strtotime()
strtotime('next tuesday');
You could probably find out if you have gone past that day by looking at the week number:
$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);
if ($weekNoNextTuesday != $weekNo) {
//past tuesday
}
I know it's a bit of a late answer but I would like to add my answer for future references.
// Create a new DateTime object
$date = new DateTime();
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
The nice thing is that you can also do this with dates other than today:
// Create a new DateTime object
$date = new DateTime('2006-05-20');
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
To make the range:
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
References
PHP Manual - DateTime
PHP Manual - DateInterval
PHP Manual - DatePeriod
PHP Manual - clone
The question is tagged "php" so as Tom said, the way to do that would look like this:
date('Y-m-d', strtotime('next tuesday'));
For some reason, strtotime('next friday') display the Friday date of the current week. Try this instead:
//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14
$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07
You can use Carbon library.
Example: Next week friday
Carbon::parse("friday next week");
PHP 7.1:
$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();
PHP manual getTimestamp()
Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:
Module Module1
Sub Main()
Dim d As Date = Now
Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
Console.WriteLine("next friday is " & nextFriday)
Console.ReadLine()
End Sub
End Module
If I understand you correctly, you want the dates of the next 7 days?
You could do the following:
for ($i = 0; $i < 7; $i++)
echo date('d/m/y', time() + 86400 * $i);
Check the documentation for the date function for the format you want it in.
if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.
var date=new Date();
getNextDayOfWeek(date, 2);
// this is for finding next tuesday
function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
Hope this will be helpfull to you, thank you
The PHP documentation for time() shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.