vanamerongen
vanamerongen

Reputation: 837

Adding different amounts of time to dates

I'm making a function in which I'm trying to store data in different date formats. I want to store the data in months or weeks depending on how many days there are. So basically, I mean to say that if I fetch data from two months, I want it to store the data in an array per week, like:

'1 2013' => 'some data',
'2 2013' => 'some data',
'3 2012' => 'some data',

And if I'm fetching data for more than three months, I want to store the data per month, so:

'Jan 2013' => 'some data',
'Feb 2013' => 'some data',
'Mar 2012' => 'some data',

Wanted to take into account that I might want to add more scenarios, so I made a conditional to determine the way the data is stored depending on the amount of days. It works with months, but for some reason it doesn't work with weeks. I keep adding a week to the date in a loop to get the next date. When I check the type of the $time variable after I call strtotime(), it says it's a boolean type. Don't understand why!

This is the relevant part of my code (edited bits out for clarity):

$times = array();
$length = 90;                   //Fetched data for 90 days.
$periods= round($length/7);     //Want to divide it up into weeks.
$per = 'week';
$format= 'W Y';

for($n = 0; $n < $periods; $n++){
    $time               = date($format, $start);

       //Here's apparently where it goes wrong:

    $time               = date($format, strtotime($time . ' + ' . $n . $per));
    $times[$time] = array();
}

e: wanted to add, even when I do it like this:

$time = date($format, strtotime($time . ' + 1 week'));

It won't store the proper value.

Upvotes: 1

Views: 83

Answers (4)

Jarek.D
Jarek.D

Reputation: 1294

Might be good idea to change a logic here and iterate over result items and 'put' them straight away into right periodic groupings so (not tested but i'm sure you'll get the idea):

$times = Array();

// apply format for groupings
$format= 'W Y'; 

// traverse results item by item
foreach($results as $item){

    // assumed content of result here - this has to be adapted
    $timestamp = $item['timestamp'];
    $value = $item['value'];
    $key = date($format, $timestamp);

    // new grouping so create new key
    if(!isset($times[$key])) $times[$key] = Array();

    // add item
    $times[$key][] = $value;
}

Upvotes: 1

Benz
Benz

Reputation: 2335

I hope this example can help you; When I am working with dates, I like the DateTime object, please take a look at the code below (I Hope the comments explain it enough)

$times = array(); //array to hold values in

$Start = new DateTime('01-01-2013'); //the start date, may come from db??

$End = new DateTime('01-02-2013'); //the end date (You can also create a new DateTime object and add 90 days if you want)


$Diff = $Start->diff($End); //check how many days are between the two dates, if higher than 90, format by month, else by week

if ($Diff->days > 90) { //from 90 days, show per month instead of weeks
    $per = 'month';
    $format = 'm Y';
} else {
    $per = 'week';
    $format = 'W Y';
}

while($Start < $End) { //loop until $Start equals or is higher than $End

    $times[$Start->format($format)] = 'some data'; //push data into the array 

    $Start->modify('+1 ' . $per); //add X amount of time to the $Start variable 
}


print_r($times);

And this code shows:

Array
(
    [01 2013] => some data
    [02 2013] => some data
    [03 2013] => some data
    [04 2013] => some data
    [05 2013] => some data
)

Upvotes: 2

Guerra
Guerra

Reputation: 2790

The format accepted to strtotime is string, so you need to convert your $time to string before adding date.

So,

 $time               = date($format, strtotime($time->format('Y-m-d')->toString() . ' + ' . $n .' '. 'weeks')); //i think it might be plural "weeks" no "week"

Upvotes: 0

pmcgovern
pmcgovern

Reputation: 71

Read up on this functionality: PHP Manual: DateTime:add

Upvotes: -1

Related Questions