Question

So I'm trying to take a date I have stored in a string, and increment it by one week 9 times. (I want 10 dates total).

The value of $start_date is initially: 2013-12-04 12:00, but when I increment it it becomes:

606813 
then: 
1211613
1816413 and so on.

I understand that the formatting will change, but I don't understand why I am getting the results that I do. The second returned number seems to imply:

12(month)11(day)6(some unkown thing)13(year)

which would be a week, but the first and third returned numbers doesn't make sense at all unless there is a 60th and 18th month of the year I'm not aware of, and they're all missing a time to go along with the date.

This is my first attempt to use strtotime() so I'm not sure what I'm doing, but I have tried to work through this and I could use any help you can offer.

$start_date = $row["start_time"];

for ( $i = 1; $i<9; $i++) {
    $start_date= strtotime('+1 week', $start_date);
    $arr = array('id' => $row["course_id"],
        'title' => $row["course_name"],
        'start' => $start_date,
        'allDay' => false
    );
    array_push($result, $arr);
}
Was it helpful?

Solution

strtotime returns int and the second parameter (if used) should be a valid timestamp. Also if you want to get the date string you can use date function

$start_date = strtotime('2013-12-04 12:00');

for ( $i = 1; $i<9; $i++){
    $start_date= strtotime('+1 week', $start_date);
    $arr = array('id' => $row["course_id"],
             'title' => $row["course_name"],
             'start' => date('Y-m-d h:i:s',$start_date),
             'allDay' => false
             );
        array_push($result, $arr);
    }

OTHER TIPS

To increment time with strtotime I had to first convert the start date before any increments are done, otherwise it considers $startdate to be the "epoch" and creates dates in the 70s.

              //changed here
$start_date = strtotime($row["start_time"]);

for ( $i = 1; $i<9; $i++){
    $start_date= strtotime('+1 week', $start_date);
    $arr = array('id' => $row["course_id"],
                 'title' => $row["course_name"],
                 'start' => $start_date,
                 'allDay' => false
                 );
            array_push($result, $arr);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top