PHP get same day of same week next year

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

I've tried various things to get this to work (such as using DateTime to advance by a year and find "previous" of whatever day of the week it was). But every model I've tried breaks down a bit after just a few years. They'll end up on like...the second Friday of the month., See the edit on my answer, I think that will give you what you want! It should go forward a year and find the nearest same weekday. So if a year from now is a Saturday, it will go a year from now minus one day. – Andrew May 23 '14 at 22:21 ,I've modified the function below to find the closest day of the week instead of the next one (though it tends to just be the same as the previous one except subtracting)., For some reason on that date it did not go forward exactly one year. I had to check to see if the day of the week matched before modifying it. Not sure why PHP handled that date that way and I'd like to find out why. – John Conde May 23 '14 at 20:53

My suggestion would be to schedule the next meeting for the same day in the same ISO 8601 week number the following year. The following function will do that for you:-

/**
 * @param \DateTime $date Date of the original meeting
 * @return \DateTime Date of the next meeting
 */
function getSameDayNextYear(\DateTime $date = null) {
   if (!$date) {
      $date = new\ DateTime();
   }
   return (new\ DateTime()) - > setISODate((int) $date - > format('o') + 1, (int) $date - > format('W'), (int) $date - > format('N'));
}
load more v
88%

Observe the following remarks when the current day-of-week is the same as the day-of-week used in the date/time string. The current day-of-week could have been (re-)calculated by non-relative parts of the date/time string however. , "'last' dayname 'of' " takes the last dayname of the current month. (Example: "last wed of july 2008" means "2008-07-30") , It sets the day-of-month to 1. , This page describes the different relative date/time formats that the strtotime(), DateTime and date_create() parser understands.

load more v
72%

$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).
   ' day', strtotime($date)));
load more v
65%

Sometimes we work with days of the week and need to know the last/next occurrence of, for example, a Saturday:,Sometimes you want to find the last occurence of a day before today, or before yesterday or before 2 days ago. The following examples show the syntax you can use for this:,I have just been doing some work with dates today and this page has helped me immensely. Thank you,Another problem is when you want to find the previous or next occurence of a certain day of the week. For example, today being a Saturday we can run the following:

For my server;

date('l jS F (Y-m-d)', strtotime('+$kac days'))

date('l jS F (Y-m-d)', strtotime('+$kac days'))

You need to use double quotes when the string contains a variable:
date('l jS F (Y-m-d)', strtotime("+{$kac} days"));

date('l jS F (Y-m-d)', strtotime("+{$kac} days"));

In PostgreSQL you can use:

... BETWEEN CURRENT_DATE - INTERVAL '5 years + 1 day' AND CURRENT_DATE +
INTERVAL '5 years + 1 day' ...

...BETWEEN CURRENT_DATE - INTERVAL '5 years + 1 day'
AND CURRENT_DATE +
   INTERVAL '5 years + 1 day'...

Something like this should work:

$date = date('Y-m-d', strtotime('-6 hours'))
$time = date('H:i:s');

$date = date('Y-m-d', strtotime('-6 hours'))
$time = date('H:i:s');

This might be what you're after:

$last_saturday_this_month = date('l jS F', strtotime("next month last Saturday"));

$last_saturday_this_month = date('l jS F', strtotime("next month last Saturday"));

Wondering if you could help me understand why the results of this code is off by one (1) for actual day of year?

echo date('z', strtotime('2012-5-9'));
// shows 129 and s/b 130?

echo date('z', strtotime('2012-5-9'));
// shows 129 and s/b 130?

It's really not as complicated as you think:

$dayofweek = date('l', strtotime('26 feb 2028')); // Saturday

$dayofweek = date('l', strtotime('26 feb 2028')); // Saturday

<?PHP
for($year=2000; $year <= 2050; $year++) {
echo date('l jS F, Y', strtotime("{$year}-07-01 second Saturday")) . "<br>";
}
?>

<?PHP
  for($year=2000; $year <= 2050; $year++) {
    echo date('l jS F, Y', strtotime("{$year}-07-01 second Saturday")) . "<br>";
  }
?>
load more v
75%

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date.,strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.Syntax:,date() Function: The date() function returns more understandable and human readable format of date.Syntax:,timestamp: It is the default time variable from which the date will be generated.

strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.
Syntax:

strtotime($EnglishDateTime, $time_now)

date() Function: The date() function returns more understandable and human readable format of date.
Syntax:

date(format, timestamp)
Output:
First day of this week: Monday - 04 / 02 / 2019
Output:
First day of this week: Sunday - 03 / 02 / 2019
First day of last week: Sunday - 27 / 01 / 2019
First day of next week: Sunday - 10 / 02 / 2019
First day of week after next week: Sunday - 17 / 02 / 2019
load more v
40%

MAKEDATE(year,dayofyear) , This section describes the functions that can be used to manipulate temporal values. See Section 11.2, “Date and Time Data Types”, for a description of the range of values each date and time type has and the valid formats in which values may be specified. , The nondeterministic nature of SYSDATE() also means that indexes cannot be used for evaluating expressions that refer to it. , Here is an example that uses date functions. The following query selects all rows with a date_col value from within the last 30 days:

When invoked with the INTERVAL form of the second argument, ADDDATE() is a synonym for DATE_ADD(). The related function SUBDATE() is a synonym for DATE_SUB(). For information on the INTERVAL unit argument, see Temporal Intervals.

mysql > SELECT DATE_ADD('2008-01-02', INTERVAL 31 DAY); -
> '2008-02-02'
mysql > SELECT ADDDATE('2008-01-02', INTERVAL 31 DAY); -
> '2008-02-02'

When invoked with the days form of the second argument, MySQL treats it as an integer number of days to be added to expr.

mysql > SELECT ADDDATE('2008-01-02', 31); -
> '2008-02-02'
load more v

Other "undefined-undefined" queries related to "PHP get same day of same week next year"