Get first/last day of week in php?
Something like this:
<?php
date_default_timezone_set("Asia/Kolkata");
$today = date("Y-m-d");
$mon = new DateTime($today);
$sun = new DateTime($today);
$mon->modify('last Monday');
$sun->modify('next Sunday');
var_dump($mon);
var_dump($sun);
$first_day_of_week = $mon->format("Y-m-d");
$last_day_of_week = $sun->format("Y-m-d");
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
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 #2Your 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 PHP Find the first day-of-week in a given time period
Just a little math with the date('N')
(day of the week) value:
$timestamp = strtotime('2011-11-08');
$weekday = date('N', $timestamp);
if ($weekday > 1) {
$timestamp = strtotime('+' . abs(8 - $weekday) . ' days', $timestamp);
}
echo date('D Y-m-d', $timestamp);
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! 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 );
?>
Finding First day of week via php
strtotime
is quite powerful with relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(this only works with PHP 5.3+)strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime
and date
(date('t')
gives the number of days of the month):mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
If you just want to get a string for presentation, then you don't need mktime
:date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Related Topics
PHP Prepared Statements and Transactions in a Loop
How to Find "Related Items" in PHP
Reverse Letters in Each Word of a String Without Using Native Splitting or Reversing Functions
Determine If User Is Using Proxy
Setting PHP Enviromental Variable While Running Command Line Script
How to Use a PHP Class from Another File
How to Make a Chat Room Script with PHP
How to Require PHP Files Relatively (At Different Directory Levels)
Phpmyadmin Failed to Set Session Cookie. Maybe You Are Using Http Instead of Https
Codeigniter Active Record Not Equal
Get First Element in PHP Stdobject
Facebook Graph API - Friends Using Application
Make Script Execution to Unlimited
How to Make Ruby Aes-256-Cbc and PHP Mcrypt_Rijndael_128 Play Well Together
PHP - Display a 404 Error Without Redirecting to Another Page