Get whole week date starting from sunday to saturday from given date

vasdev chandrakar

Hello guys i am working in php and my requirement is to get complete week dates from given date as i need to calculate weekly working hour. And week must be started from sunday to saturday not monday to sunday. I have code which works properly for other days of week except sunday. it means if give any dates from monday to saturday it works properly but if i give sunday's date it give last week's dates. please check my code and advise me for better solution.

$days = array();                
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("W");
$y =    date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";             
for($day=0; $day<=6; $day++)
{
    $days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}               
print_r($days);
Rahul

From source,

Here is the snippet you are looking for,

// set current date
$date = '01/03/2018';
// parse about any English textual datetime description into a Unix timestamp 
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday 
for ($i = 0; $i < 7; $i++, $ts += 86400){
    print date("m/d/Y l", $ts) . "\n";
}

Here is working demo.

If you need normal standard format code,

Here is your snippet,

// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow    = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset * 86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
    print date("Y-m-d l", $ts) . "\n";
}

Here is working demo.

EDIT

As per your requirement, now week will start from sunday to saturday

<?php
// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Sunday
$dow    = date('w', $ts);
$offset = $dow;
if ($offset < 0) {
    $offset = 6;
}
// calculate timestamp for the Sunday
$ts = $ts - $offset * 86400;
// loop from Sunday till Saturday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
    print date("Y-m-d l", $ts) . "\n";
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get Last week date range from sunday to saturday moment js

分類Dev

SQLite: Get week number from date

分類Dev

How to get date from week number and day in sql?

分類Dev

How to Define date format from a given date

分類Dev

Reduce 100 years from a given date in php

分類Dev

Get tomorrows date from the webpage

分類Dev

Check if a date is Sunday

分類Dev

Get month list starting from a given month and ending to another?

分類Dev

How can I get the right day of the week, if I am setting the date in relation to the given query string parameters?

分類Dev

How to get maximum date value from a group of date columns

分類Dev

Get date, month and year value from Date string

分類Dev

Delete a object from array upto given date javascript

分類Dev

How to get month name from date in Presto

分類Dev

How to get expiration date from a gpg key

分類Dev

Get day number from a specific date

分類Dev

C# - Get Date from Int Value

分類Dev

Get calculated date from moment object react

分類Dev

how to get current date from server

分類Dev

Get records happening on a particular date from mysql

分類Dev

Get Date from String Containing Timestamp

分類Dev

java programming get the date from a calendar

分類Dev

How to get a proper date from a text?

分類Dev

Get network usage from specific date on terminal

分類Dev

Get this week's monday's date in Postgres?

分類Dev

Excel - Get Week Day Start Date as formula

分類Dev

How to get which day of week is a specific date?

分類Dev

How to get date range for current week?

分類Dev

SQL Get date of first day of previous week

分類Dev

how get the last week start date and end date based on date in current week ruby

Related 関連記事

ホットタグ

アーカイブ