PHP strtotime +1 week error - php

I have a problem with strtotime and the following code (I want all calendar weaks in the given time range)
$all_dates = array();
$tempdate = '2014-01-01';
$enddate = '2015-03-01';
while (strtotime($tempdate) <= strtotime($enddate)) {
$all_dates[] = date('Y-W', strtotime($tempdate));
$tempdate = date('Y-m-d', strtotime($tempdate . '+1 week'));
}
var_dump($all_dates);
the var_dump produces the following output
array (
0 => '2014-01',
1 => '2014-02',
2 => '2014-03',
3 => '2014-04',
4 => '2014-05',
5 => '2014-06',
6 => '2014-07',
7 => '2014-08',
8 => '2014-09',
9 => '2014-10',
10 => '2014-11',
11 => '2014-12',
12 => '2014-13',
13 => '2014-14',
14 => '2014-15',
15 => '2014-16',
16 => '2014-17',
17 => '2014-18',
18 => '2014-19',
19 => '2014-20',
20 => '2014-21',
21 => '2014-22',
22 => '2014-23',
23 => '2014-24',
24 => '2014-25',
25 => '2014-26',
26 => '2014-27',
27 => '2014-28',
28 => '2014-29',
29 => '2014-30',
30 => '2014-31',
31 => '2014-32',
32 => '2014-33',
33 => '2014-34',
34 => '2014-35',
35 => '2014-36',
36 => '2014-37',
37 => '2014-38',
38 => '2014-39',
39 => '2014-40',
40 => '2014-41',
41 => '2014-42',
42 => '2014-43',
43 => '2014-44',
44 => '2014-45',
45 => '2014-46',
46 => '2014-47',
47 => '2014-48',
48 => '2014-49',
49 => '2014-50',
50 => '2014-51',
51 => '2014-52',
52 => '2014-01',
53 => '2015-02',
54 => '2015-03',
55 => '2015-04',
56 => '2015-05',
57 => '2015-06',
58 => '2015-07',
59 => '2015-08',
60 => '2015-09',
)
The Problem is entry number 52 with '2014-01', it should be '2015-01'. With the timerange 2015-01-01 till 2016-03-01 the result is ok. Is this a bug in strtotime function?
I'm using PHP version 5.5.10
Thanks for your help.

Change this line and use the o iso year:
$all_dates[] = date('o-W', strtotime($tempdate));
// ^

This has to do with ISO week numbers. Also, strtotime() is a bad way to do date math and PHP recommends against it (thanks to leap years and daylight savings). Use the DateTime() classes to do this with accuracy:
$startdate = new DateTime('2014-01-01');
$enddate = new DateTime('2015-03-01');
$interval = new DateInterval('P7D');
$period = new DatePeriod($startdate , $interval, $enddate);
foreach($period as $date) {
$all_dates[] = $date->format('o-W'); // o modifier for ISO year
}
var_dump($all_dates);
Demo

Related

I want to remove the common elements in two arrays using PHP [duplicate]

This question already has answers here:
How to remove an array value from another array using PHP question
(3 answers)
Closed 2 years ago.
I tried the following code and it should be working, but not getting the required result. What's wrong with the code? I have two arrays and I want to remove the common elements in both arrays so I wore the following code.
<?php
$aMgaMembersList= array (
0 => '9194962',
1 => '9197448',
2 => '9174039',
3 => '9199473',
4 => '9175598',
5 => '9197474',
6 => '9195444',
7 => '9195268',
8 => '9189438',
9 => '9175103',
10 => '9199619',
11 => '9195267',
12 => '9194463',
13 => '9196333',
14 => '9197471',
15 => '9198479',
16 => '9197472',
17 => '9185479',
18 => '9197452',
19 => '9197442',
20 => '9180861',
21 => '9194950',
22 => '9198464',
23 => '9199613',
24 => '9175939',
25 => '9195442',
26 => '9190203',
27 => '9199613',
) ;
$aRocketMembersList = array (
0 => '9174039',
1 => '9175103',
2 => '9175598',
3 => '9175939',
4 => '9180861',
5 => '9185479',
6 => '9189438',
7 => '9190203',
8 => '9194463',
9 => '9194950',
10 => '9194962',
11 => '9195267',
12 => '9195268',
13 => '9195442',
14 => '9195444',
15 => '9196333',
16 => '9197442',
17 => '9197448',
18 => '9197452',
19 => '9197471',
20 => '9197472',
21 => '9197474',
22 => '9198464',
23 => '9198479',
24 => '9199473',
25 => '9199613',
26 => '9199619',
27 => 'arun',
) ;
if (is_array($aRocketMembersList)) {
foreach ($aRocketMembersList as $rocketUsername) {
if (in_array($rocketUsername, $aMgaMembersList)) {
unset($aMgaMembersList[array_search($rocketUsername, $aMgaMembersList)]);
unset($aRocketMembersList[array_search($rocketUsername, $aRocketMembersList)]);
}
}
}
print_r($aRocketMembersList);
print_r($aMgaMembersList);
The out put is
Array
(
[27] => arun
)
Array
(
[27] => 9199613
)
The element 9199613 shouldn't be there. Why it's happening? I ran the code in a different environment and the result is same.
Here's a different function that works regardless of the order of Arrays:
<?php
function different($array1, $array2){
$m = array_merge($array1, $array2); $x = array_intersect($array1, $array2); $a = array_diff($m, $x); $b = array_diff($x, $m); $a = array_merge($a, $b);
$r = [];
foreach($a as $v){
$o = new StdClass; $k = array_search($v, $array1);
if($k === false)$k = array_search($v, $array2);
$o->$k = [$array1[$k], $array2[$k]]; $r[] = $o;
}
return $r;
}
$aMgaMembersList = [
0 => '9194962',
1 => '9197448',
2 => '9174039',
3 => '9199473',
4 => '9175598',
5 => '9197474',
6 => '9195444',
7 => '9195268',
8 => '9189438',
9 => '9175103',
10 => '9199619',
11 => '9195267',
12 => '9194463',
13 => '9196333',
14 => '9197471',
15 => '9198479',
16 => '9197472',
17 => '9185479',
18 => '9197452',
19 => '9197442',
20 => '9180861',
21 => '9194950',
22 => '9198464',
23 => '9199613',
24 => '9175939',
25 => '9195442',
26 => '9190203',
27 => '9199613'
];
$aRocketMembersList = [
0 => '9174039',
1 => '9175103',
2 => '9175598',
3 => '9175939',
4 => '9180861',
5 => '9185479',
6 => '9189438',
7 => '9190203',
8 => '9194463',
9 => '9194950',
10 => '9194962',
11 => '9195267',
12 => '9195268',
13 => '9195442',
14 => '9195444',
15 => '9196333',
16 => '9197442',
17 => '9197448',
18 => '9197452',
19 => '9197471',
20 => '9197472',
21 => '9197474',
22 => '9198464',
23 => '9198479',
24 => '9199473',
25 => '9199613',
26 => '9199619',
27 => 'arun'
];
$diffArray = different($aMgaMembersList, $aRocketMembersList);
$test = json_encode($diffArray);
?>
$tmp1 = array_diff($aMgaMembersList,$aRocketMembersList);
$tmp2 = array_diff($aRocketMembersList,$aMgaMembersList);
$final = array_unqiue(array_merge($tmp1, $tmp2));
unset($tmp1);unset($tmp2);//and maybe original arrays?
There are probably better solutions, but that should work for you. If you had associative arrays instead of numeric values you could exclude array_unqiue
EDIT:
I originally assumed you wanted the results in one array, if that's unnecessary just use the array_diff function twice, and you can maintain your original array names as desired. Again there are probably better solutions (more memory/processor efficient), but in most practical cases this will be fine. If you're working with extremely large data sets... do more research ;)

How to Create complex Array Structure in PHP

I have to make this kind of structure in array;
We have three ( 3 ) variables which creates this structure:
$numberOfParticipants = 38; // 38 is example
$numberOfParticipantsPerHeat = 8 // 8 is example
$numberOfHeats = 5; // 5 is example
Based on this variables I have this table:
The problem is that, I can't place the ' - ' or null after 31 OR 38. The task is that i have to make the arrays of array "almost equal" like the photo and must depend on the variables above. By the way, after I create the correct list I will slice the array to 5 or 6 or whatever parts I need this is not the problem, the problem is that I have to parse the list like this first. This is what I tried so far:
$calc1 = (int)round($numberOfParticipants * $numberOfParticipantsPerHeat, -1); //First round the numberOfParticipants to closest integer by 10
$readyArr = [];
for ($i = 1; $i <= $calc1; $i++) {
if ($i <= $numberOfParticipants) {
$readyArr[$i] = $i;
} else {
$readyArr[$i] = null;
}
}
The problem with this snippet is that it places the null at the end of the list not after 31, or based on the var.
This is the result I have:
array:40 [▼
1 => 1
2 => 2
3 => 3
4 => 4
5 => 5
6 => 6
7 => 7
8 => 8
9 => 9
10 => 10
11 => 11
12 => 12
13 => 13
14 => 14
15 => 15
16 => 16
17 => 17
18 => 18
19 => 19
20 => 20
21 => 21
22 => 22
23 => 23
24 => 24
25 => 25
26 => 26
27 => 27
28 => 28
29 => 29
30 => 30
31 => 31
32 => 32
33 => 33
34 => 34
35 => 35
36 => 36
37 => 37
38 => 38
39 => null
40 => null
]
The Array after partition I want should be:
array(
0 => array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6, 6 => 7, 7 => 8,),
1 => array(0 => 9, 1 => 10, 2 => 11, 3 => 12, 4 => 13, 5 => 14, 6 => 15, 7 => 16,),
2 => array(0 => 17, 1 => 18, 2 => 19, 3 => 20, 4 => 21, 5 => 22, 6 => 23, 7 => 24,),
3 => array(0 => 25, 1 => 26, 2 => 27, 3 => 28, 4 => 29, 5 => 30, 6 => 31, 7 => null,),
4 => array(0 => 32, 1 => 33, 2 => 34, 3 => 35, 4 => 36, 5 => 37, 6 => 38, 7 => null,),
);
Every help, every clue will be highly appreciated.
There are two things you need to know about the target structure:
How many players are in the first (which will always be the largest, if only by one) set.
$playersPerHeat = ceil($numberOfParticipants / $numberOfHeats);
// note this replaces your hard-coded $numberOfParticipantsPerHeat
You also need to know how many heats actually have that many, that is how many heats are actually full.
$fullHeats = $numberOfParticipants % $numberOfHeats ?: $numberOfHeats;
// The ?: bit means that if we get zero (ie. all equal heats), then we
// count all the heats instead, since they're all full.
Now it's easy!
$players = range(1,$numberOfParticipants);
$heats = array_merge(
array_chunk(
array_slice($players, 0, $fullHeats * $playersPerHeat),
$playersPerHeat
),
array_chunk(
array_slice($players, $fullHeats * $playersPerHeat),
$playersPerHeat - 1
)
);
That's it! Demo

output days of the week via function

help me please
I have a code
<?php
// date on Russian
function getDateRus(){
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
return ( (int)date('d') . ' ' . $monthes[(date('n'))] . date(' Y'));
}
// day on Russian
function getDayRus(){
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
return $days[(date('w'))];
}
echo "Today:" . getDateRus() . ", " . getDayRus();
?>
I need to put days off for a week ahead:
25 September, Monday
26 September, Tuesday
27 September, Wednesday
28 September, Thursday
29 September, Friday
30 September, Saturday
01 October, Sunday
I can print the dates for the week ahead, but without the Russian language.
how to deduce the dates for the week ahead and save the Russian language?
I changed your structure slightly and I made use of the powerful function DateTime
$monthes = array(
1 => 'Января', 2 => 'Февраля', 3 => 'Марта', 4 => 'Апреля',
5 => 'Мая', 6 => 'Июня', 7 => 'Июля', 8 => 'Августа',
9 => 'Сентября', 10 => 'Октября', 11 => 'Ноября', 12 => 'Декабря'
);
$days = array(
'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
'Четверг', 'Пятница', 'Суббота'
);
$dArray = array();
$date = new DateTime();
for($i=0; $i<7;$i++){
$dArray[] = $date->format("d").' '.$monthes[$date->format("n")].','.$days[$date->format("w")];
$date->modify("+1 day");
}
print_r($dArray);
Output:
Array ( [0] => 25 Сентября,Понедельник [1] => 26 Сентября,Вторник [2] => 27 Сентября,Среда [3] => 28 Сентября,Четверг [4] => 29 Сентября,Пятница [5] => 30 Сентября,Суббота [6] => 01 Октября,Воскресенье )
You can probs achieve this better by using strftime and setlocale. That way you will not need your day/month arrays

Changing date type with PHP

Here's my code:
$date_depo = date('d-m-Y', strtotime($date_depot));
So in my excel table I have => 2 nov 16 00:00:00
and this code changes it to => 02-11-2016
But if I have 10 mai 13 00:00:00 it changes it to 01-01-1970
it means that the value is 0 or something like that, how can I change it to => 10-05-2013
Edit:
setlocale (LC_TIME, 'fr_FR.utf8','fra');
$date_depo = strftime('%Y-%m-%d', $date_depot);
Doesn't work.
$trans = array('janv' => 'January ','févr' => 'February','mars' => 'March ','avr' => 'April','mai' => 'May','juin' => 'June','juil' => 'July','août' => 'August','sept' => 'September','oct' => 'October','nov' => 'November','déc' => 'December');
$resulat= strtr($date_depot,$trans);
$date_depot_t = date('d-m-Y', strtotime($resulat));
Had to replace every month, but it works!

store monthly date into array from the selected date

Assume that selected date from Canlender is 02/09/2011. To store weekly date into array from 20/09/2011 is
for($i=0; $i<7; $i++)
{
$WeeklyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
My question is how to store monthly date into array from the selected date.
Many thanks
---Update----------
The final result of monthlyDate should look like the following:
$monthlyDate= array{2011-08-03, 2011-08-04, 2011-08-05, 2011-08-06, 2011-08-07 ....2011-08-31, 2011-09-01, 2011-09-02}
First, calculate the number of days in a month using cal_days_in_month and then proceed as you are doing with weeks eg:
$days = cal_days_in_month(CAL_GREGORIAN, 9, 2011);
for($i = 0; $i <= $days; $i++)
{
$MonthlyDate[] = date("Y-m-d", strtotime(2011-09-02) - 86400*$i);
}
Notice that CAL_GREGORIAN is a built-in constant.
Working Example
Whenever programs are incrementing a date using 86400 there is a risk of unexpected output because of DST.
By using strtotime() with a unit larger than hours (like days, weeks, months, etc.) preventing any DST hiccups. Note: a DateTime object approach can be used but for this case, it is unnecessary overhead.
The following is an adjusted form of a one-liner date range function I developed.
Here is the online demo for this case.
function getDatesFromRange($a,$b,$x=0,$dates=[]){
while(end($dates)!=$b && $x=array_push($dates,date("Y-m-d",strtotime("$a +$x day"))));
return $dates;
}
$date='2011-09-02';
$monthlyDate=getDatesFromRange(date("Y-m-d",strtotime("$date -1 month +1 day")),$date);
var_export($monthlyDate);
output as desired/expected:
array (
0 => '2011-08-03',
1 => '2011-08-04',
2 => '2011-08-05',
3 => '2011-08-06',
4 => '2011-08-07',
5 => '2011-08-08',
6 => '2011-08-09',
7 => '2011-08-10',
8 => '2011-08-11',
9 => '2011-08-12',
10 => '2011-08-13',
11 => '2011-08-14',
12 => '2011-08-15',
13 => '2011-08-16',
14 => '2011-08-17',
15 => '2011-08-18',
16 => '2011-08-19',
17 => '2011-08-20',
18 => '2011-08-21',
19 => '2011-08-22',
20 => '2011-08-23',
21 => '2011-08-24',
22 => '2011-08-25',
23 => '2011-08-26',
24 => '2011-08-27',
25 => '2011-08-28',
26 => '2011-08-29',
27 => '2011-08-30',
28 => '2011-08-31',
29 => '2011-09-01',
30 => '2011-09-02',
)

Categories