Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first day of a given week number in PHP (multi-platform)?

Tags:

php

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.

like image 488
Amadeus45 Avatar asked Nov 02 '09 05:11

Amadeus45


People also ask

How do I get the start of the week 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. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How do I print the day of the week 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 a week start and end in php?

getStartAndEndDate($week, $year); output: $return[0] = $firstDay; $return[1] = $lastDay; 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.

How can I get the date of a specific day with php?

You can use the date function. I'm using strtotime to get the timestamp to that day ; there are other solutions, like mktime , for instance.


6 Answers

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.

like image 169
roshanbh Avatar answered Oct 02 '22 23:10

roshanbh


Yet another solution:

<?php
        $week = 3;
        $year = 2009;

        $timestamp = mktime( 0, 0, 0, 1, 1,  $year ) + ( $week * 7 * 24 * 60 * 60 );
        $timestamp_for_monday = $timestamp - 86400 * ( date( 'N', $timestamp ) - 1 );
        $date_for_monday = date( 'Y-m-d', $timestamp_for_monday );
?>
like image 34
Alan Haggai Alavi Avatar answered Oct 03 '22 00:10

Alan Haggai Alavi


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

like image 24
Jonas Avatar answered Oct 03 '22 00:10

Jonas


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);
like image 27
cletus Avatar answered Oct 02 '22 23:10

cletus


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

$time = new DateTime();
$time->setISODate(2016, 13);
for($i=0;$i<7;$i++){
    echo $time->format('d-M-Y') . '<br />';
    $time->add(new DateInterval('P1D'));
}
like image 33
jcromeros1987 Avatar answered Oct 03 '22 00:10

jcromeros1987


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

<?php

  $week = 4;
  $year = 2013;

  $timestamp_for_monday = mktime( 0, 0, 0, 1, 1,  $year ) + ((7+1-(date( 'N', mktime( 0, 0, 0, 1, 1,  $year ) )))*86400) + ($week-2)*7*86400 + 1 ;

?>

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

like image 38
spf Avatar answered Oct 03 '22 00:10

spf