getting the numeric value of "this Monday" - php - php

I'm trying to display a numeric value of the current week,
in a calendar-like feature that I have on a website I'm working on.
So, the function that I came up with in the end, working, is...
echo date("d", strtotime("this Tuesday"));
All goes well for most of the days,
except that, for example, today is Tuesday, the 25th, all week dates display well
since Tuesday to Sunday (25 - 30),
but for Monday it will say 01, because I think it thinks that "this Monday" is the 01 of December, not yesterday. This Monday usually should be the Monday of this week, yesterday (24), but the function when you apply "this Monday" will display the next Monday, because today it's already Tuesday, and yesterday is out of the scope ?
But this is weird, because i'm reffering to "this Monday", Monday of this week. It displays the next Monday. Is that a bug in the date function ?
How do I safely get the values of the days of the week otherwise?
thanks!

strtotime("Monday this week");
From the manual:
reltext space 'week' | Handles the special format "weekday + last/this/next week". | "Monday next week"

Try it with "last Monday" and "next Monday" keywords.
For Example:
echo date("d", strtotime("last Monday"));
another example...
echo date("d", strtotime("next Monday"));

Related

PHP stringtotime function first weekday

echo date("d-M-Y", strtotime("first monday 2019-07"));
Shouldn't the output of this line be 01-Jul-2019? Thats the first monday in July but the output is: 08-Jul-2019.
What is the error im making?
Try
first monday of 2019-07
Instead of first monday 2019-07.
Use,
echo date("d-M-Y", strtotime("first monday of 2019-07"));
You can find here php instructions to parsing date as natural languages.
http://php.net/manual/en/datetime.formats.relative.php
" first monday 2019-07 " returns the date of the first monday of july excluding the start date (in this case 1st july 2019). Where as "first monday of 2019-07" returns the date of the first monday of july including the start date.

how to find a date from the value of week of the month in laravel..?

I need to find first, second, etc, week in a month.
In that week, i need to find a Monday/Tuesday or else.
I'm using laravel framework and php,
Now, i'm going to find Monday of first week of this September month and
my code is,
date('Y-m-d', strtotime('+0 week, Monday', strtotime('first day of September 2016')));
This is correct way or not..?
Here, the answer is no date or 5-9-2016 ..?
I would higly recommend you to read about Carbon, it comes by default with Laravel and is what is used internaly to deal with dates. Also is really handy and easy to use.
It's much simpler than you think using strtotime. I believe the last few are what you're after. You'll have to be careful though, some weeks in partiular months may not have a particular day. For instance the 1st week of November 16 does not have a Monday.
<?php
echo date("Y-m-d",strtotime('first monday September 2016'));
// 2016-09-05 (first Monday of september '16)
echo date("Y-m-d",strtotime('Tuesday +1 week December 2016'));
// 2016-12-06 (first Tuesday of December '16)
echo date("Y-m-d",strtotime('Friday November 2016'));
// 2016-11-04 (first Friday of November '16')
echo date("Y-m-d",strtotime('fourth Monday September 2016'));
// 2016-09-05 (first Monday of september '16)
echo date("Y-m-d",strtotime('Tuesday December 2016'));
// 2016-12-06 (first Tuesday of December '16)
echo date("Y-m-d",strtotime('third Friday November 2016'));
// 2016-11-04 (first Friday of November '16')
echo date("Y-m-d",strtotime('Tuesday +1 week December 2016'));
// 2016-12-06 (Tuesday of the second week of December '16)
echo date("Y-m-d",strtotime('Friday +2 week November 2016'));
// 2016-11-04 (Friday of the third week of November '16')
?>
Without the '+n week' in the last 2 examples, it's going to return the first matching day in the first week. Adding +1 week makes it the second week it chooses the day from. Adding +2 week makes it the third week it chooses from, etc.
But like I mentioned above, it's not foolproof. For instance:
echo date("Y-m-d",strtotime('Friday +4 week November 2016'))
// 2016-12-02 Although there are 5 calendar weeks in November '16, the 5th
// week does not have a Friday. So you're getting December's Friday, because
// you're simply adding weeks to a day.

Get day of the next week by monday with php

i want to get the day of the next week with php..i need something like this
If today is monday, i want to display the days (es 18-19-20) of the next week, not of this week..the week start on monday...but it will work also if today is sunday..always the days of the next week..i can't found anything because i tried to use jddayofweek but i don't understand it maybe..i haven't found an italian guide..
I have a table which display the days in stringn form..so sunday monday etc, fir my problem i tried date() function but if i use +1 on the day i will have problem with february for example
Not sure exactly what you're doing, but maybe something like:
echo date("d", strtotime("monday next week"))
This will give you next Monday:
$date = date_create()->modify('monday next week');
echo date_format($date, "d");

PHP strtotime +1 month adding an extra month [duplicate]

This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 9 years ago.
I have a simple variable that adds one month to today:
$endOfCycle = date("Y-m", strtotime("+1 month"));
Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March.
It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date.
This will happen on the 31st of a lot of months as well, but is obviously more noticable in the case of January to Feburary because Feb is shorter.
If you're not interested in the day of month and just want it to give the next month, you should specify the input date as the first of the current month. This will always give you the correct answer if you add a month.
For the same reason, if you want to always get the last day of the next month, you should start by calculating the first of the month after the one you want, and subtracting a day.
This should be
$endOfCycle=date('Y-m-d', strtotime("+30 days"));
strtotime
expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
while
date
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
See the manual pages for:
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.date.php
You can use this code to get the next month:
$ts = mktime(0, 0, 0, date("n") + 1, 1);
echo date("Y-m-d H:i:s", $ts);
echo date("n", $ts);
Assuming today is 2013-01-31 01:23:45 the above will return:
2013-02-01 00:00:00
2
today is 29th of January, +1 month means 29th of Fabruary, but because February consists of 28 days this year, it overlaps to the next day which is March 1st
instead try
strtotime('next month')
Maybe because its 2013-01-29 so +1 month would be 2013-02-29 which doesn't exist so it would be 2013-03-01
You could try
date('m/d/y h:i a',(strtotime('next month',strtotime(date('m/01/y')))));
from the comments on http://php.net/manual/en/function.strtotime.php
$endOfCycle = date("Y-m", mktime(0, 0, 0, date("m", time())+1 , 15, date("m", time())));
try this:
$endOfCycle = date("Y-m", time()+2592000);
this adds 30 days, not exactly a month tough.

Get Last Monday - Sunday's dates: Is there a better way?

I'm preparing a query for mySQL to grab record from the previous week, but I have to treat weeks as Monday - Sunday. I had originally done this:
WHERE YEARWEEK(contactDate) = YEARWEEK(DATE_SUB(CURDATE(),INTERVAL 7 DAY))
to discover that mySQL treats weeks as Sunday - Monday. So instead I'm parsing getting the begin & end dates in php like this:
$i = 0;
while(date('D',mktime(0,0,0,date('m'), date('d')-$i, date('y'))) != "Mon") {
$i++;
}
$start_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+7), date('y')));
$end_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+1), date('y')));
This works - it gets the current week's date for monday (walking backwards until a monday is hit) then calculates the previous week's dates based on that date.
My question is: Is there a better way to do this? Just seems sloppy, and I expect someone out there can give me a cleaner way to do it - or perhaps not because I need Monday - Sunday weeks.
Edit
Apparently, there is:
$start = date('Y-m-d',strtotime('last monday -7 days'));
$end = date('Y-m-d',strtotime('last monday -1 days'));
That's about a million times more readable. Thank you.
you can use strtotime for this kind of date issues
echo strtotime("last Monday");
(complementing on marvin and Stomped ) Also you can use it this way
echo date('Y-m-d',strtotime('-1 Monday')); //last Monday
echo date('Y-m-d',strtotime('-2 Monday')); //two Mondays ago
echo date('Y-m-d',strtotime('+1 Monday')); //next Monday
strtotime("previous week Monday")
Monday of the previous week.
Marvin's answer is really elegant, although if you really wanted to go for performance you could do it with a little arithmetic. You could derive a formula/method to convert an arbitrary date to "days since some starting point" (probably 01.01.0000) and then the rest of the operations would be easy with that. Getting the day of week from such a number is as simple as subtracting and getting the remainder of a division.
Actually, PHP had a Date class in its PEAR library which did exactly this.
I have to point out for all the readers here there is a big issue with marvin's answer
Here is the catch
The "last Monday" function will return date the "latest" Monday.It will be explained like this , if today is Monday, then it will return the date of "LAST WEEK" Monday, however if today is not Monday, it will return "THIS WEEK" Monday
The Question is request to return "Last Week" Monday. Therefore the result will be incorrect if today is not Monday.
I have solved the issue and wrapped in my Date Time Helper
It will be only one line after you "INCLUDE" the class
$lastWeekMonday = Model_DTHpr::getLastWeekMonday();
Check Here
https://github.com/normandqq/Date-Time-Helper

Categories