Laravel php的date_add函数不起作用

时间:2016-01-29 11:41:47

标签: php laravel-5

制作了这款Laravel播种机功能。问题是日期不会像预期的那样改变。如果我在Tinker中运行代码就可以了。

我做错了什么?

public function run() {
    $faker = Faker::create();

    foreach(range(1, 10) as $index)
    {
        $myDateCalled = $faker->dateTime($max = 'now');

        $myDateAllocated = $myDateCalled;
        $i = $faker->numberBetween($min = 5, $max = 300);
        date_add($myDateAllocated, date_interval_create_from_date_string("{$i} seconds"));

        $myDatePickedUp = $myDateAllocated;
        $i = $faker->numberBetween($min = 5, $max = 30);
        date_add($myDatePickedUp, date_interval_create_from_date_string("{$i} minutes"));

        $myDateDelivered = $myDatePickedUp;
        $i = $faker->numberBetween($min = 10, $max = 90);
        date_add($myDateDelivered, date_interval_create_from_date_string("{$i} minutes"));


        dd($myDateCalled, $myDateAllocated, $myDatePickedUp, $myDateDelivered);

        Hhtransport::create([
            'Called' => $myDateCalled,
            'Allocated' => $myDateAllocated,
            'PickedUp' => $myDatePickedUp,
            'Delivered' => $myDateDelivered,
            'From' => $faker->numberBetween($min = 1, $max = 99),
            'To' => $faker->numberBetween($min = 1, $max = 99),
            'Put' => $faker->numberBetween($min = 1, $max = 99),
            'Drev' => $faker->numberBetween($min = 1, $max = 99),
            'Container' => $faker->numberBetween($min = 1, $max = 99)
        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

dateTime是一个对象,因此通过引用传递。含义我只创建一个具有4个不同名称的dateTime对象。

使用$ myDateAllocated = clone $ myDateCalled;解决了我的问题。