PHP – How to get start date and end date from given week number and year

PHP – How to get start date and end date from given week number and year

In this post we will give you information about PHP – How to get start date and end date from given week number and year. Hear we will give you detail about PHP – How to get start date and end date from given week number and yearAnd how to use it also give you demo for it if it is necessary.

In this PHP tutorial, I will tell you how to get start date and end date from week number and year.

We will calculate ISO week number (1-53) and represent the start of the week “Monday”.

There is a setISODate() method with DateTime extension that accepts the year (4-digit year) and ISO week number.


Get start and end date from week number

  1. <?php
  2. $week=29;
  3. $year=2017;
  4. functiongetStartAndEndDate($week,$year){
  5. $dateTime=newDateTime();
  6. $dateTime->setISODate($year,$week);
  7. $result['start_date']=$dateTime->format('d-M-Y');
  8. $dateTime->modify('+6 days');
  9. $result['end_date']=$dateTime->format('d-M-Y');
  10. return$result;
  11. }
  12. $dates=getStartAndEndDate($week,$year);
  13. print_r($dates);
  14. ?>
<?php
$week=29;
$year=2017;

function getStartAndEndDate($week, $year) {
  $dateTime = new DateTime();
  $dateTime->setISODate($year, $week);
  $result['start_date'] = $dateTime->format('d-M-Y');
  $dateTime->modify('+6 days');
  $result['end_date'] = $dateTime->format('d-M-Y');
  return $result;
}

$dates=getStartAndEndDate($week,$year);
print_r($dates);

?>

If you run above code then you will get following output :

Array
(
    [start_date] => 17-Jul-2017
    [end_date] => 23-Jul-2017
)


Get date from week number

  1. <?php
  2. $week=29;
  3. $year=2017;
  4. $timestamp=mktime(,,,1,1,$year)+($week*7*24*60*60);
  5. $timestampForMonday=$timestamp-86400*(date('N',$timestamp)-1);
  6. $dateForMonday=date('d-M-Y',$timestampForMonday);
  7. echo$dateForMonday;
  8. ?>
<?php
$week=29;
$year=2017;
$timestamp = mktime( 0, 0, 0, 1, 1,  $year ) + ( $week * 7 * 24 * 60 * 60 );
$timestampForMonday = $timestamp - 86400 * ( date( 'N', $timestamp ) - 1 );
$dateForMonday = date( 'd-M-Y', $timestampForMonday );
echo $dateForMonday;
?>

Output :

17-Jul-2017


Get start and end date from week number using Carbon

If you have installed carbon extension then you can use following code to get same output :

  1. <?php
  2. $week=29;
  3. $year=2017;
  4. $date=CarbonCarbon::now();
  5. $date->setISODate($year,$week);
  6. echo$date->startOfWeek();
  7. echo$date->endOfWeek();
  8. ?>
<?php
  $week=29;
  $year=2017;
  $date = CarbonCarbon::now();
  $date->setISODate($year,$week);
  echo $date->startOfWeek();
  echo $date->endOfWeek();
?>
Show Demo

Hope this code and post will helped you for implement PHP – How to get start date and end date from given week number and year. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  67  =  73

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US