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',
)
Related
array:23 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 8
6 => 9
7 => 10
8 => 11
9 => 12
10 => 15
11 => 16
12 => 17
13 => 18
14 => 19
15 => 22
16 => 23
17 => 24
18 => 25
19 => 26
20 => 29
21 => 30
22 => 31
]
this is a array of working days apart from Sunday and Saturday and i have a table of months data and i need a Laravel where condition for comparing all data with whe
->whereYear('Clock_Day',$yearofdata)
->whereMonth('Clock_Day',$monthofdata)
->whereIn('Clock_Day','=',$workdays) //here can i use something like whereIn->whereday---for comparing all array values and get as per the data
There is no whereDayIn() or similar method, but you can do this:
->whereYear('Clock_Day', $yearofdata)
->whereMonth('Clock_Day', $monthofdata)
->where(function($q) use($workdays) {
foreach ($workdays as $day) {
$q->whereDay('Clock_Day', '=', $day, 'or');
}
})
If Clock_Day is a field in your table, you will need to extract the date part you are comparing for each piece (Year, Month, Day). You could possibly use whereIn with DB::raw:
->where(DB::raw('YEAR("Clock_Day")'),$yearofdata)
->where(DB::raw('MONTH("Clock_Day")'),$monthofdata)
->whereIn(DB::raw('DAYOFMONTH("Clock_Day")'),$workdays)
You would only use whereYear or whereMonth to compare those values against fields in your table called 'year' and 'month', but since you want to use the same 'Clock_Day' field for all comparisons you need to extract the relevant data for each part.
Simple question here. Why is this giving an error?
for ($k = 1; $k < 17; $k++){
echo $result[0]->answer_."$k";
}
Edit: contents of $result:
Array ( [0] => stdClass Object ( [entry_id] => 37 [answer_1] => 20-24 yrs old [answer_2] => No [answer_3] => Yes [answer_4] => No [answer_5] => 236.7 [answer_6] => Ideal blood pressure – upper figure between 91 & 120, lower figure between 61& 80 [answer_7] => No, I haven’t had it checked [answer_8] => No, I quit more than 10 years ago [answer_9] => I sometimes get 150 minutes or more per week [answer_10] => 2-5 hours per day [answer_11] => Every day [answer_12] => Less often or never [answer_13] => Every day [answer_14] => 3-4 days a week [answer_15] => Never [answer_16] => imperial ) )
Try
for ($k = 1; $k < 17; $k++){
echo $result[0]->{'answer_'.$k};
}
Updated based on comment from mpen below
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)
How can I get the time period (day, week, month) by a given timestamp? I do not want the date. I want the next time period based on the amount of seconds.
Is there a PHP native function for that?
Example:
$period = getTimeperiod( 15638400 );
My attempt: I could check and count the seconds:
if x <= 60 => min
if x <= 60*60 => hour
if x <= 60*60*24 => day
...
Edit:
Time period means either minute, hour, day, week, ... as stated above... ?! So all I want is the corresponding time period for a timestamp.
Example: (day = 86400 secs) then a timestamp with getTimeperiod( 85000 ) should be "day".
I think you're searching for something like the class DateInterval ...
It is part of PHP since 5.3.0 and has a static function called createFromDateString() where you can set up a DateInterval from a string like "3600 seconds". From this object you then can get the day, month, year and so on.
Take a look at this page:
http://www.php.net/manual/de/dateinterval.createfromdatestring.php
But is this on the right path returning an interval object (period)? Cred to #SimonSimCity for pointing out DateInterval. If you guide me I could improve the answer.
<?php
$timestamp = 15638400;
echo "The timestamp $timestamp is " . date("Y-m-d H:i:s", 15638400) . "<br \>";
echo "<pre>";
print_r (DateInterval::createFromDateString(date("Y \\y\\e\\a\\r\\s m \\m\\o\\n\\t\\h\\s d \\d\\a\\y\\s\\ H \\h\\o\\u\\r\\s i \\m\\i\\n\\u\\t\\e\\s s \\s\\e\\c\\o\\n\\d\\s", 15638400 ) ) );
echo "</pre>"
?>
Outputting
The timestamp 15638400 is 1970-07-01 00:00:00
DateInterval Object
(
[y] => 1970
[m] => 7
[d] => 1
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
I solved it that way:
/*
seconds 0
minutes 1
hours 2
days 3
week 4
month 5
year 6
decade 7
century 8
millenium 9
*/
$arTimes = array(
0 => 1,
1 => 60,
2 => 60*60,
3 => 60*60*24,
4 => 60*60*24*7,
5 => 60*60*24*7*4,
6 => 60*60*24*7*4*12,
7 => 60*60*24*7*4*12*10,
8 => 60*60*24*7*4*12*10*10,
9 => 60*60*24*7*4*12*10*10*10
);
$nDiff = strtotime( $nTo ) - strtotime( $nFrom );
switch( $nDiff )
{
// check difference and time period
case $nDiff <= $arTimes[ 1 ]:
$nGranularity = 0;
break;
...
}
I have some date, like:
20 November 06:10
12 November 08:12
10 October 13:23
There all in the past 6 months, Now I want to strtotime() them, but they are all un complete (lack of year), how to make some process so that I could strtotime() them?
Try this:
$dates = array("10 October 13:23", "12 November 08:12", "10 October 13:23");
foreach($dates as $d){
$exploded = explode(" ", $d);
$newDate = array_slice($exploded, 0,2,true)+array(2=>"2012")+array(3 => $exploded[2]);
//print_r($newDate);
$time = strtotime(implode($newDate));
echo $time."<br/>";
}
The output i got is:
1349868180
1352704320
1349868180
The logic is:
You lack the year, so I exploded the dates into an array to slice them, insert the year (the +array(2=>"2012") part) and glue them again with implode, and then run the strtotime.
This work only for this year, so you can use this logic to add the year to all your dates, or in the future there will be absolutely no way to filter dates from different years.
I added the dates into an array for loop through all of them, you can use the loop other ways, depending on where you have all your dates stored. For example if they are in a database you can include the script in the while($row = mysqli_fetch_assoc($result)) part where $d would be $row['date'] instead.
You should use the DateTime class and its createFromFormat and getTimeStamp methods instead of strtotime.
print_r(date_parse_from_format("d F H:i", '20 November 06:10'));
gives you:
Array
(
[year] =>
[month] => 11
[day] => 20
[hour] => 6
[minute] => 10
[second] => 0
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] =>
)