Get first/last day of week in php?
Something like this:
$mon = $fri = new DateTime('2017-05-23');
$mon->modify('Last Monday');
$fri->modify('Next Friday');
var_dump($mon);
var_dump($fri);
Get first day date of the week on a given date PHP
You can do the following:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example:
http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
Get first day of current week - x
$weeks = 8;
// Timestamp for $weeks weeks ago
$time = strtotime("$weeks weeks ago");
// Day of the week for $time (1 - Mon, ...)
$week_day = date('N', $time);
// Number of days from Monday
$diff = $week_day - 1;
// The date of the Monday $weeks weeks ago
echo date('j', $time - ($diff * 24 * 3600));
How to get the first day of a given week number in PHP (multi-platform)?
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 );
?>
PHP - get first day of a given week number when week starts on day other than Monday
Use DateTime::setISODate
, where 3rd parameter is day of the week:
$dt = new DateTime;
echo $dt->setISODate(2014, 1, 0)->format('Y-m-d'), "\n"; # 0 = Sunday
echo $dt->setISODate(2014, 1, 1)->format('Y-m-d'), "\n"; # 1 = Monday
echo $dt->setISODate(2014, 1, 6)->format('Y-m-d'), "\n"; # 6 = Saturday
demo demo #2
Your example isn't working, because you are using format yyyyWweek
, which is by default Monday. Read about ISO-8601 formats, and you will see that there is format yyyy-Wweek-day
, which you can use like:
$format = sprintf("%d-W%02d-%d", $year, $weeknumber, $weekstarts == 7 ? 0 : $weekstarts);
$first_day_of_week = strtotime($format);
echo "$format returned $first_day_of_week\n";
demo
Getting first / last date of the week
According to docs the format strings "first day of" and "last day of" are only allowed for months, not for weeks. See http://www.php.net/manual/en/datetime.formats.relative.php
If you combine first and last day of with a week statement the result either blows the parser or is something that you did not expect (usually the first or last day of a month, not a week).
The difference that you see between Win and Linux is probably only because of different error reporting settings.
To get the first and last day of the current week use:
$date->modify('this week');
$date->modify('this week +6 days');
To get the first and last day of week 50 use:
$date->setISODate(2011, 50);
$date->setISODate(2011, 50, 7);
EDIT:
If you want to use the modify method for absolute week numbers you have to use the formats defined in http://www.php.net/manual/en/datetime.formats.compound.php:
$date->modify('2011W50');
$date->modify('2011W50 +6 days');
How to set the first day of the week to Thursday in PHP
http://php.net/manual/en/datetime.formats.relative.php says that as of PHP version 5.6.23, 7.0.8 "Weeks always start on monday. Formerly, sunday would also be considered to start a week." That said, is your problem that the number of weeks returned might be incorrect depending on whether today falls on or before Thursday of the current week? Maybe try something like this:
$date = new DateTime();
$week = intval($date->format('W'));
$day = intval($date->format('N'));
echo $day < 4 ? $week-1 : $week;
If subtracting 1 isn't the answer you could play around with addition/subtraction, comparing the result with the actual answer you know to be true until you get the right formula. Hope this helps!
Related Topics
Forcing a Simplexml Object to a String, Regardless of Context
How to Delete a Line from the File With PHP
MySQL Prepared Statements With a Variable Size Variable List
PHP.Ini & Smtp= - How to Pass Username & Password
Submitting a Multidimensional Array Via Post With PHP
How to Remove File Extension from a Website Address
MySQLi: Can It Prepare Multiple Queries in One Statement
PHP Xml How to Output Nice Format
Selecting Unique Values from a Column
Reading Very Large Files in PHP
Turning Multidimensional Array into One-Dimensional Array
How to Define a Class Property Value Dynamically in PHP
Preserving Session Variables Across Different Domains
How to Start and End Transaction in MySQLi
List All the Files and Folders in a Directory With PHP Recursive Function
How to Remove Line Breaks (No Characters!) from the String