Get date of week php
I've seen some variants on this question but I believe this one hasn't been answered yet. Show I need to get the starting date and ending date of a week, chosen by year and week number (not a date) example: input:
output:
The return value will be something like an array in which the first entry is the week starting date and the second being the ending date. OPTIONAL: while we are at it, the date format needs to be I've tried editing existing functions that almost did what I wanted but I had no luck so far. Please help me out, thanks in advance.
Mat 198k40 gold badges385 silver badges400 bronze badges asked Feb 1, 2011 at 10:24
1 Using DateTime class:
Returns:
Explained:
A shorter version (works in >= php5.3):
Could be shortened with class member access on instantiation in >= php5.4. answered Dec 16, 2013 at 22:30
user3108829user3108829 1,6912 gold badges10 silver badges3 bronze badges 4 Many years ago, I found this function:
answered Feb 1, 2011 at 11:41
Roham RafiiRoham Rafii 2,8037 gold badges34 silver badges44 bronze badges 0 We can achieve this easily without the need for extra computations apart from those inherent to the DateTime class.
The answered Apr 21, 2016 at 8:54
0 Slightly neater solution, using the "[year]W[week][day]" strtotime format:
answered Jan 20, 2013 at 23:26
tripper54tripper54 3462 silver badges6 bronze badges shortest way to do it:
answered Jul 5, 2015 at 15:37
Claudiu CreangaClaudiu Creanga 7,65110 gold badges64 silver badges106 bronze badges You can get the specific day of week from date as bellow that I get the first and last day
answered Mar 5, 2019 at 5:20
The calculation of Roham Rafii is wrong. Here is a short solution:
if you want the last day of the week number, you can add up answered Feb 7, 2015 at 16:45
This is an old question, but many of the answers posted above appear to be incorrect. I came up with my own solution:
answered May 6, 2015 at 20:51
bezzbezz 1,26817 silver badges28 bronze badges 0 First we need a day from that week so by knowing the week number and knowing that a week has seven days we are going to do so the
this way pickAday will be a day in our desired week. Now because we know the year we can check which day is that. things are simple if we only need dates newer than unix timestamp We will get the unix timestamp for the first day of the year and add to
that If we want to find out the same thing of let's say 12th week of 1848 we must use another approach as we can not get the timestamp. Knowing that each year a day advances 1 weekday meaning (1st of november last year was on a sunday, this year is on a monday, exception for the leap years when it advances 2 days I believe, you
can check that ). What I would do if the year is older than 1970 than make a difference between it and the needed year to know how many years are there, calculate the day of the week as my pickADay was part of 1970, shift it back one weekday for each.
Nandu 3,0007 gold badges31 silver badges51 bronze badges answered Feb 1, 2011 at 10:57
Catalin MarinCatalin Marin 1,2421 gold badge11 silver badges18 bronze badges 1 For documentation (since Google ranks this question first when searching for "php datetime start end this week"). If you need the startdate and enddate for the current week (using DateTime):
Hope this will help. answered Jun 28, 2018 at 9:44
AvatarAvatar 13.3k8 gold badges112 silver badges183 bronze badges Have you tried PHP relative dates? It might work. answered Feb 1, 2011 at 10:33
MikelMikel 24.1k8 gold badges63 silver badges66 bronze badges 2 Even if you dont want to use a specific date you cannot escape it. You can calculate a week based on the date ONLY. Steps:
I would recommend working with a consistent dates sistem like Zend_Date or Pear Date. answered Feb 1, 2011 at 11:26
Elzo ValugiElzo Valugi 26.3k14 gold badges91 silver badges114 bronze badges
answered Aug 14, 2013 at 18:44
1
will print
hopefully will help answered Jun 21, 2018 at 9:28
The "first day of the week" is subjective. Some cultures use "Monday" others "Sunday", maybe others something else? For my purposes, I want the first day of the week to be "Sunday" and the last day of the week to be "Saturday". Also, using Furthermore the string "sunday this week" does not seem to be reliable. It actually will return Sunday the next week (according to my view of what a week is). I've built a method which returns a PHP object containing two DateTime objects. One for the first day (Sunday) of the given week, the second for the last day (Saturday) of the given week.
answered Aug 12, 2019 at 15:16
jeremysawesomejeremysawesome 6,8455 gold badges33 silver badges37 bronze badges How do you get the day of the week from a date in PHP?If your date is already a DateTime or DateTimeImmutable you can use the format method. $day_of_week = intval($date_time->format('w'));
How can I get current date from next week in PHP?just used it - date('Y-m-d H:i:s', strtotime('+1 week')); works fine in PHP 7 to give one week from now.
How can I get Monday of current week in PHP?Try this. echo date('Y-m-d', strtotime('last monday', strtotime('next monday'))); It will return current date if today is monday, and will return last monday otherwise.
How can I get last week start and end date in PHP?date("m/d/Y", strtotime("last week monday")); date("m/d/Y", strtotime("last week sunday")); It will give the date of Last week's Monday and Sunday.
|