Getting week numbers for last X weeks - php

I have a script that builds an array of week numbers for the last 12 weeks like so:
$week_numbers = range(date('W'), date('W')-11, -1);
However, if the current week number is 1, then this will return an array like so:
Array
(
[0] => 1
[1] => 0
[2] => -1
[3] => -2
[4] => -3
[5] => -4
[6] => -5
[7] => -6
[8] => -7
[9] => -8
[10] => -9
[11] => -10
)
But I need this array to look like this instead:
Array
(
[0] => 1
[1] => 52
[2] => 51
[3] => 50
[4] => 49
[5] => 48
[6] => 47
[7] => 46
[8] => 45
[9] => 44
[10] => 43
[11] => 42
)
Can anyone see a simple solution to this?
I have thought about doing something like this (not tested):
$current_week_number = date('W');
if($current_week_number<12){
// Calculate the first range of week numbers (for current year)
$this_year_week_numbers = range(date('W'), 1, -1);
// Calculate the next range of week numbers (for last year)
$last_year_week_numbers = range(52, 52-(11-$current_week_number), -1);
// Combine the two arrays to return the week numbers for the last 12 weeks
$week_numbers = array_merge($this_year_week_numbers,$last_year_week_numbers);
}else{
// Calculate the week numbers the easy way
$week_numbers = range(date('W'), date('W')-11, -1);
}

one idea
$i = 1;
while ($i <= 11) {
echo date('W', strtotime("-$i week")); //1 week ago
$i++;
}

if you arent scared of loops you can do this:
$week_numbers = range(date('W'), date('W')-11, -1);
foreach($week_numbers as $key => $value) { if($value < 1) $week_numbers[$key] += 52; }

You can do a modulo % trick:
$week_numbers = range(date('W'), date('W')-11, -1);
foreach ($week_numbers as $i => $number) {
$week_numbers[$i] = (($week_numbers[$i] + 52 - 1) % 52) + 1;
}
// -1 +1 is to change the range from 0-51 to 1-52
I've found that using modulo like this is often useful for date calculations, you can something similar for months, using 12.

Well, I think the easiest way is to create array after getting dates:
$week_numbers = array_map(function($iDay)
{
return ($iDay+52)%52?($iDay+52)%52:52;
}, range(date('W'), date('W')-11));
-note, that you can not do just % since 52%52 will be 0 (and you want 52)

Related

PHP: print all days between a given day and end of month

I don't want to return DATE (Y-m-d).
I need to print all days until end of month from a given day independently of the month or year.
I tried both [$array as $i] - [$array as $key] and didn't work.
$myday (for example = 19)
return $days
would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28
I would need each value for $days to compare each to another field.
Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....
EDITING
Need something very simple like this one:
$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}
But print_r won't do it, I need to return as each value to use in different if conditions
This is easily done using DateTime(), DateInterval(), DatePeriod(), and relative date formats.
$start = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$days = array();
foreach($period as $date) {
$days[] = $date->format('d');
}
Results
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
)
Demo

CodeIgniter 2.1, PHP - find a day in array, take that day plus six more and remove the rest from the array

I have an array which represent all month. How can I find today's day in the array, take that day, plus next six days (not necessary six days) and remove rest from the array?
For example:
$days = array('1', '2', '3', ... '28', '29', '30');
I need 3-9. How can I do this?
I think you should look at another way of doing whatever it is you want to do. But here is a solution for what you asked:
$days = range(1, date('t'));
$days_forward = 6;
$key = array_search(date('j'), $days);
if($key === FALSE)
die('Date not found in array');
$days = array_slice($days, $key, $days_forward + 1);
print_r($days);
Output:
Array
(
[0] => 19
[1] => 20
[2] => 21
[3] => 22
[4] => 23
[5] => 24
[6] => 25
)

Multidimensional Array Between Dates and Total Price

I'm trying to make PHP array and here is my example.
In each array first row is unique record number "[767962]"
After inside that array hotel room id 1st row.
2nd is hotel name
3rd and 4th are first price range between
5th is the price
Array
(
[767962] => Array /// --secial id for price record--//
(
[0] => 42923 /// --hotelroomid--//
[1] => Crystal Hotels Kemer Deluxe Resort /// --Hotel Name--//
[2] => 2013-04-01 /// --Start date--//
[3] => 2013-05-16 /// --End Date--//
[4] => 179 /// --Price --//
)
[767964] => Array
(
[0] => 42923
[1] => Crystal Hotels Kemer Deluxe Resort
[2] => 2013-05-17
[3] => 2013-05-26
[4] => 239
)
[767980] => Array
(
[0] => 42940
[1] => Rixos Deluxe Resort
[2] => 2013-03-02
[3] => 2013-05-26
[4] => 340
)
)
Here is the expected output.
I would like to know total price between that hotel room 2013-05-14 - 2013-05-21
1 , 42923 , Crystal Hotels Kemer Deluxe Resort , 2013-05-14 , 2013-05-21 , 1553
2 , 42940 , Rixos Deluxe Resort , 2013-05-14 , 2013-05-21 , 2380
For example 1553 is the total price between 2013-05-14 - 2013-05-21 dates.
2013-05-14 - 179
2013-05-15 - 179
2013-05-16 - 239
2013-05-17 - 239
2013-05-18 - 239
2013-05-19 - 239
2013-05-20 - 239
2013-05-21 - 239
Total is 1553
Bro i did this kind of work in my application i think this will help you mostly but if something going wrong please let me know i will change.......
For example please refer this link for Demo;
$room_id = 42923;
$start_date = "2013-05-10 00:00:00";
$end_date = "2013-05-21 00:00:00";
find dates between above two dates
$day = 86400; // Day in seconds
$format = 'Y-m-d'; // Output format (see PHP date funciton)
$sTime = strtotime($start_date); // Start as time
$eTime = strtotime($end_date); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}
Now for each date you need to check in which array it falls
foreach($days as $key => $d)
{
foreach($data as $key1 => $dat)
{
if($dat[0] == $room_id) {
if($d >= $dat[2] && $d<= $dat[3]) {
$amount += $dat[4];
}
}
}
}
echo $amount;

Insert spaces between string in array

I have the following array:
array('16 HOURS','13.3 HOURS','10.6 HOURS AGO','8 HOURS AGO','5.3 HOURS AGO','2.6 HOURS AGO','CURRENT')
I want to insert empty cells ('') between every string until the desired length is reached. I have tried various loops, for i etc but always end up with white spaces behind CURRENT or before 16 HOURS, thanks in advance.
Try this.
$array = array('16 HOURS','13.3 HOURS','10.6 HOURS AGO','8 HOURS AGO','5.3 HOURS AGO','2.6 HOURS AGO','CURRENT');
$i = 1;
foreach($array as $val) {
$tempArray[] = $val;
if($i < count($array)) {
$tempArray[] = '';
}
$i++;
}
print_r($tempArray);
Result
Array
(
[0] => 16 HOURS
[1] =>
[2] => 13.3 HOURS
[3] =>
[4] => 10.6 HOURS AGO
[5] =>
[6] => 8 HOURS AGO
[7] =>
[8] => 5.3 HOURS AGO
[9] =>
[10] => 2.6 HOURS AGO
[11] =>
[12] => CURRENT
)
$myArray = array(
'16 HOURS',
'13.3 HOURS',
'10.6 HOURS AGO',
'8 HOURS AGO',
'5.3 HOURS AGO',
'2.6 HOURS AGO',
'CURRENT'
);
$newArray = array_combine(
range(0,12,2),
$myArray
) +
array_fill_keys(
range(1,12,2),
''
)
;
ksort($newArray);
var_dump($newArray);

PHP, multidimensional arrays: to intersected events with days of the month

I want to build a movie timetable. I have $array1 containing the movie titles and airing dates and times:
array =
0: array =
Title: string = American Beauty
Date: string = 25/09/2012
Time: string = 15:00 - 16:20
1: array =
Title: string = The Godfather
Date: string = 25/09/2012
Time: string = 16:20 - 18:20
2: array =
Title: string = Pulp Fiction
Date: string = 26/09/2012
Time: string = 15:00 - 16:20
And I have $array2 containing the days of the month grouped by Mondays, Tuesday s, Wednesday s, Thursday s and Fridays (no movies during the weekend)
Array
(
[1] => Array
(
[0] => 3
[1] => 10
[2] => 17
[3] => 24
[4] =>
)
[2] => Array
(
[0] => 4
[1] => 11
[2] => 18
[3] => 25
[4] =>
)
[3] => Array
(
[0] => 5
[1] => 12
[2] => 19
[3] => 26
[4] =>
)
[4] => Array
(
[0] => 6
[1] => 13
[2] => 20
[3] => 27
[4] =>
)
[5] => Array
(
[0] => 7
[1] => 14
[2] => 21
[3] => 28
[4] =>
)
)
I need to intersect these two arrays so I can print under day 25 the movie “American Beauty” also under day 25 “The Godfather” and under day 26 “Pulp Fiction”.
Meaning I need to print:
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3 4 5
10 11 12
17 18 19
24 25 26
15:00-16:20 American Beauty 15:00-16:20 Pulp Fiction
16:20-18:20 The Godfather
My tries so far:
foreach( $array1 as $key => $value )
{
$theTime = $value['Time'];
$theTime = explode("/", $theTime );
$days[] = $theTime [0];
$months[] = $theTime [1];
}
So I have all the airing days in array $days but from here I don’t know how to follow or even if this approach is the correct one.
Here's how I get $array2:
$month = 9;
$year = 2012;
for ($i = 1; $i <= 31; $i++)
{
$timestamp = mktime(0, 0, 0, $month , $i, $year);
if (date("n", $timestamp) == $month )
{
$day = date("N", $timestamp);
// Monday 1 to Friday 5
if ($day == 1 OR $day <= 5) {
$array2[$day][] = date("j", $timestamp);
}
}
}
Please help, I’m stuck.
Thanks very much
Ok you will iterate over $array1 and parse the date of the movie.
Then, you will look inside array2 if your day is there
foreach($array1 as $movie){
$movieDate = new DateTime($movie);
//as you indexed $array2 with monday = 1 -> friday = 5 you can use the "w" format
if(isset($array2[$movieDate->format("w"))){
$array[$movieDate->format("j")][] = $movie;
}
}
I made some mutation in your output array, it becomes :
SEPTEMBER 2012
Monday Tuesday Wednesday ....
3=>[] 4 =>[] 5=>[]
10=>[] 11 =>[] 12=>[]
17=>[] 18 =>[] 19=>[]
24 =>[] 25=>[ 26 =>[
15:00-16:20 American Beauty, 15:00-16:20 Pulp Fiction]
16:20-18:20 The Godfather]

Categories