Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding different amounts of time to dates
    text
    copied!<p>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:</p> <pre><code>'1 2013' =&gt; 'some data', '2 2013' =&gt; 'some data', '3 2012' =&gt; 'some data', </code></pre> <p>And if I'm fetching data for more than three months, I want to store the data per month, so:</p> <pre><code>'Jan 2013' =&gt; 'some data', 'Feb 2013' =&gt; 'some data', 'Mar 2012' =&gt; 'some data', </code></pre> <p>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! </p> <p>This is the relevant part of my code (edited bits out for clarity):</p> <pre><code>$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 &lt; $periods; $n++){ $time = date($format, $start); //Here's apparently where it goes wrong: $time = date($format, strtotime($time . ' + ' . $n . $per)); $times[$time] = array(); } </code></pre> <p>e: wanted to add, even when I do it like this:</p> <pre><code>$time = date($format, strtotime($time . ' + 1 week')); </code></pre> <p>It won't store the proper value.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload