A
A
Alexander2016-07-31 11:52:28
Laravel
Alexander, 2016-07-31 11:52:28

Why does the time() function run in a loop return the same value?

Elementary code:

for ($i = 0; $i < 20; $i++){
            echo time() . '<br>';
        }

Here is the output:
74f5e7385fd44bf09f4233c3876a3ba7.png
Version php7. Why is this happening? after all polzhno to return different value. Local project.
And here is what I want to do:
foreach ($files as $file) {
            $name = time();
            $file->move(public_path('upload/' . $path_name . '/' . $id), $name . '.' . $file->getClientOriginalExtension());
        }

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
Vladimir Grabko, 2016-07-31
@kentuck1213

1.microtime(true);
2. do not do this. Suddenly 2 files will load at the same time. Name the hash of the file

A
Alexey Ukolov, 2016-07-31
@alexey-m-ukolov

Enjoy:

for ($i = 0; $i < 20; $i++){
    echo time() . '<br>';
    sleep(1);
}

M
Mikhail Osher, 2016-07-31
@miraage

It's hard to guess that the loop takes milliseconds?
Write microtime(true) - check out the difference.

R
Rsa97, 2016-07-31
@Rsa97

RTFM: tempnam

#
#algooptimize #bottize, 2016-07-31
@user004

Returns the number of seconds that have elapsed since the beginning of the Unix Epoch (The Unix Epoch, January 1, 1970 00:00:00 GMT) until the current time.
Apparently this number has not changed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question