Do you need a PHP programmer?
Use my services as a freelance PHP programmer by hiring me to do PHP programming on your website project.

I have built many custom PHP applications like project managers, classified ad websites and content management systems. I also work with open source applications such as WordPress, online shopping cart websites like Magento and develop content management systems like Joomla.

Thursday, January 24, 2013

Get date of one week based on week number - php

Hi, ,
There are many kind of php function you can use for datetime manipulation. But today I will share a short code that you can use for get date of one week based on your week number.

I have created a function that you can use or develop
Let see my function below :

<?php
 function get_date_in_week($year,$week_number){
  $date = array();
  
  // 1 week 7 days
  for($day = 1; $day <= 7; $day++){
   
   $set_date = strtotime($year."W".($week_number<10?'0':'').$week_number.$day);
   
   if(date('Y',$set_date) == $year){
    $date['year-'.$year]['week-'.$week_number]['day-'.$day] = date('j', $set_date);
   }
   
  }
  
  return $date;
  
 }
?>


Now, you can simple to use, just call the function then you will get date of one week based on your week number.

<pre>
 <?php
  $year = '2013';
  $week_number = '2';
  print_r(get_date_in_week($year,$week_number));
 ?>
</pre>


The result will be like this :

Array
(
    [year-2013] => Array
        (
            [week-2] => Array
                (
                    [day-1] => 7
                    [day-2] => 8
                    [day-3] => 9
                    [day-4] => 10
                    [day-5] => 11
                    [day-6] => 12
                    [day-7] => 13
                )

        )

)

Hope it can helps.
But if you still need help, just write your comment below.
I will try to find out.

Kind regard,
Bayu Prawira

No comments:

Post a Comment