How would I go about getting the date of say the 32 Wed of a given year. Is there an easy way to do this that I am missing?
It is worth noting, that strtotime() accepts ISO week date format (for example "2008-W27-2" is Tuesday of week 27 in 2008), so it can be easily used to get the date of a given week number.
Source
echo date("d m Y", strtotime("2010-W32-3"));
i think... :p
Edit: W32 is the week number and 3 is the day of the week (1-7)
Or maybe something more complicated like this - its a bit pointless if the above works - but was just thinking of alternatives... I don't know if it would work. :p Just for fun.
echo date("d m Y", strtotime("1-1-2010 + 32 Weeks ".date("N", "Wednesday")." Days"));
maybe, but now I'm clutching at straws ha. :)
Related
I need to know the date of next Friday. Known only today's date.
PHP / Framework Laravel 5.1
How do I know the date of the next Friday?
Thank you!
This is nice and easy using strtotime
strtotime('next friday');
See strtotime()
EDIT: Two minuses in under a minute. I thought this was a tiny but interesting query.
ORIGINAL QUERY: I am generating a five week calender into which I will pour info (the day and date and other stuff).
I want the top left cell to be the "current" Sunday.
For example if today is Weds 12th then I need to find Sun 9th as the start for the run. Then I just do a $var = strtotime("+1 day", $var) for the next 34 slots.
My problem is doing this neatly if today is Sunday.
At present I have:
date_default_timezone_set("Pacific/Honolulu");
$day_now = time();
$current_day= date ("D", $day_now);
if ($current_day == "Sun")
{$day_now = strtotime("+1 day", $day_now);}
$day_now = strtotime("last sunday");
//do stuff/
I just wondered if there was a more "tidy" way of doing this.
I tried "this sunday, last sunday, sunday this week" but could find nothing that would pick today as the Sunday and ALSO work for the rest of the week.
Just curious if anyone has found a form of words that works for this with strtotime.
OK strtotime has NO one phrase solution to the current query ('this sunday" for the whole week). (I would call it a bug!)
The linked discussions
Strange behaviour of strtotime() when using relative dates ('this week')
and
Computing relative dates in php using strtotime()
offer solutions.
I like my current solution - while not elegant it is very readable and I know that if I come back to it in a couple of years I will understand what is going on.
Hopefully "this sunday" will be fixed someday instrtotime.
Thanks for the input.
I'm trying to jump from month to month, starting from a specific timestamp, but when I get jump from August, September always gets skipped. Starting From August 31 (1346389200) and jumping 1 month:
strtotime('+1 month', 1346389200);
yields 1349067600 - which is October 1st.
I've read all about strtotime making mistakes if it doesn't have a starting date to calculate from, but what could the issue be with this?
Thanks
One month after 31 August is 31 September, but, because it not exists, php force the result to 1 Oktober.
So, you should force the current month on 1th day (of course if you want only year and month) :
strtotime('+1 month',strtotime(date("Y-m-1",1346389200)));
but if you use php >5.3 you can use more reliable DateTime class and methods.
You probably shouldn't use timestamps and strtotime for this comparison. You will introduce problems because of things like daylight savings, leap years, etc. Best to use DateTime and DateInterval classes/functions to do this in a more thorough manner.
http://php.net/manual/en/datetime.add.php
I have a string like 09-10 which is representative of mm-dd. I need it in a format something like Monday 10th September? The problem is that I do not have a year and I can't have an array containing months and days because I would like to know the day of the week (Mon, Tue, Wed etc.)
Any idea how to do this in PHP, preferably using date() to format the date?
Note: this is not in MySQL...
You can't get the day (Monday, Tuesday etc) without knowing the year.
You can use date('jS F', strtotime('2012-09-10')); to get the day of the month and month, just shove any old year in there. I'd make sure to use a leap year year though to make sure you catch those pesky feb dates properly.
Example: http://codepad.org/JfUeTQlH
So, like this:
$d_m = '09-10';
$my_date = date('jS F', strtotime('2012-'.$d_m));
As it is clear Every 1st day of year is not Sunday, it happens after regular interval So just saying a date without year is not clear about day(Monday,Tuesday...). it will give you number of day in that year i.e. out of 365days. so if you are working within a year its OK, but if it goes beyond it You wont be able to get the day(Monday,Tuesday...).
I'm trying to get the starting monday date of the actual week we are in. Like we are July 15th and would like to have the result with strtotime to return July 11th...
I've tried
strtotime("-1 weekdays");
Without luck... What could be the function for the actual week please.
Thanks
$timestamp = strotime('monday this week');
Will give you Monday! ;)
Did none of these help you to find your answer?
Get first day of week in PHP?
Finding First day of week via php
First date of a week