Php get first date of week number

What is the simplest way to do it in PHP ?

I want the date of the Monday of a given week number of a year [example : week number 3 of 2009]

Thanks !

EDIT : If you use Linux only machines, use cletus' solution, however I am looking for something that can work on Windows AND Linux.

asked Nov 2, 2009 at 5:28

Amadeus45Amadeus45

1,2282 gold badges17 silver badges28 bronze badges

It's simple on PHP 5.3

echo date['M d',strtotime['2013W15']];

where 15 is the number of week. But for the number below ten make sure it is in the format of 01, 02 for first week and second week.

answered Apr 23, 2013 at 10:53

roshanbhroshanbh

6111 gold badge5 silver badges7 bronze badges

3

Yet another solution:


answered Nov 2, 2009 at 6:05

Alan Haggai AlaviAlan Haggai Alavi

70.6k19 gold badges98 silver badges126 bronze badges

6

A nice way to get this in a clean way is by using php DateTime class.

$year = 2015;
$week_no = 1;

$date = new DateTime[];
$date->setISODate[$year,$week_no];
echo $date->format['d-M-Y']; 

This would result into : 29-12-2014

answered Jan 26, 2015 at 15:14

JonasJonas

1011 silver badge4 bronze badges

You can use strptime[] to get the time.

$time = strptime['1 23 2009', '%w %U %Y'];

This will get the time for the Monday [day 1, 0 is Sunday, 6 is Saturday] of the 23rd week of 2009. If you want to format this into a date, use date[].

$date = date['d F Y', $time];

answered Nov 2, 2009 at 5:40

cletuscletus

604k162 gold badges901 silver badges939 bronze badges

2

This next script gives the 7 days of an specific week of a year

$time = new DateTime[];
$time->setISODate[2016, 13];
for[$i=0;$iformat['d-M-Y'] . '
'; $time->add[new DateInterval['P1D']]; }

answered Oct 24, 2016 at 16:04

Seems to be working and not dependent of the server OS :


the idea is to add :

  • the timestamp of the first of January of the chosen year
  • the number of seconds to reach the end of the first week [which is 7 days minus the day of week of the 1st of January + 1 day] multiplied by the number of seconds per day
  • the number of seconds of the number of chosen weeks minus the first week and the current week
  • 1 second to reach the fist second of the current week

My example returns : 1358722801 which is the timestamp of 2013/01/21 0:00:01

answered Jan 22, 2013 at 17:04

Here is very simple solution, passing a week no and returns the date.

The ISO8601 standard states that week 1 always fall on the week where Jan 4 falls.

For example, to get a day in the 4th week of the year:

$day_in_week = strtotime["2006-01-04 + 4 weeks"]];

Then you can adjust this value to Sunday [as a starting place you can guarantee that you can find]:

// Find that day's day of the week [value of 0-6]
$wday = date['w', $day_in_week];
$offset = 6 - $wday; // How far it is from Sunday.
$sunday_in_week = $day_in_week - [$offset * [60 * 60 * 24]]; // $offset * seconds in a day

Then, you add the seconds in a day again to get Monday.

$monday_in_week = $sunday_in_week + [60 * 60 * 24];

Note: This method can occasionally have some problems with daylight savings time. A similar, and slightly safer between DST time changes, method would use the DateTime class. However, DateTime is only support in PHP 5.2.0 or later. The method above works in earlier version as well.

answered Nov 2, 2009 at 5:46

JasonJason

2,53716 silver badges20 bronze badges

Try this function

function MondayOfWeek[$WeekNumber, $Year=-1] {
    if [$Year == -1] $Year   = 0+date["Y"];
    $NewYearDate             = mktime[0,0,0,1,1,$Year];
    $FirstMondayDate         = 7 + 1 - date["w", mktime[0,0,0,1,1,2009]];
    $Dates_fromFirstMonday   = 7 * $WeekNumber;
    $Second_fromFirstMonday  = 60*60*24*[$FirstMondayDate + $Dates_fromFirstMonday];
    $MondayDay_ofWeek        = $NewYearDate + $Second_fromFirstMonday;
    $Date_ofMondayDay_ofWeek = 0+date["j", $MondayDay_ofWeek];
    return $Date_ofMondayDay_ofWeek;
}

for[$i = 0; $i 

When run, I got:

-5-12-19-26-2-9-16-23-2-9-16-23-30-6-13-20-27-4-11-18-25-1-8-15-22-29-6-13-20-27-3-10-17-24-31-7-14-21-28-5-12-19-26-2-9-16-23-30-7-14-21-28

Hope this helps.

answered Nov 2, 2009 at 6:10

NawaManNawaMan

24.5k10 gold badges49 silver badges76 bronze badges

I required same in the java script..so i converted.

function[week,year]{
var timestamp = new Date[year, 0, 1, 0, 0, 0, 0];
var dateObj=new Date[];
var val = timestamp.getTime[]; 
days=[ week * 7 * 24 * 60 * 60*1000 ];
val=val+days;
var timestamp_for_monday =val - 86400 *[[timestamp.getDay[]-1000]];
var weekdate=new Date[timestamp_for_monday];
return weekdate;
}

answered Nov 29, 2018 at 7:16

Sunil SoniSunil Soni

4433 silver badges18 bronze badges

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

How to get week start date in php?

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.

How do you determine the first day of the week?

Monday is the official first day of the week according to ISO 8601.

How can I get first and last date of current week in php?

$monday = date['d-m-Y',strtotime['last monday',strtotime['next monday',strtotime[$date]]]]; You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.

How can I get start date and end date in php?

php function days[$date1, $date2] { $date1 = strtotime[$date1]; $date2 = strtotime[$date2]; return [$date2 - $date1] / [24 * 60 * 60]; } $date1 = '20100820'; $date2 = '20100930'; echo days[$date1, $date2]; ?>

Chủ Đề