在循环中递增日期

时间:2022-10-18 18:07:21

I've done some reasearch but I did not find any issue. I try to increment a date in a loop in order to test if some files does exist. In fact I would like to make some user play each sevent days. When they played the file is created whith their IP and with the date. So we test in a loop if a file does exist with each date between this days. If it exist we return 1 else we return 0.

我做了一些研究,但我没有发现任何问题。我尝试在循环中增加日期以测试某些文件是否存在。事实上,我想让每个用户每隔七天玩一次。当他们玩这个文件时,就会创建他们的IP和日期。所以我们在一个循环中测试一个文件确实存在,每个日期之间的日期。如果存在,我们返回1,否则返回0。

I met some trouble I do not really know how to increment a date in php using aloop

我遇到了一些麻烦,我真的不知道如何使用aloop在php中增加日期

I tried something like that

我尝试过类似的东西

**function afficheTirageAusort() {

    //Initialisation des variables

    $ip = $_SERVER["REMOTE_ADDR"];
    $date_str = date('d-m-y');
    $rep = "ip/";
    if (!file_exists($rep)) {
        fopen($rep, "w+");
    }
    $fichier = $ip . $date_str . '.txt';
    $periode = 7;
    $i = 0;
    $date_jeu = 0;

    //Test de l\'existence du fichier
    while ($i <= $periode) {

        list($d,$m,$Y)= explode('-',$date_str);
        $date2 = Date('d-m-Y', mktime(0, 0, 0, $m, $d + 1, $Y));
        $date = Date($date2, mktime(0, 0, 0, $m, $d + 1, $Y));
        var_dump($date);
        if (file_exists($rep . $ip . $date . '.txt')) {
            $var = 0;
        } else {
            fopen($rep . $ip . $date . '.txt', 'w+');
            $var = 1;
            //break 1;
        }
                $i++;
    };
    return $var;
}

I'm a beginner in php.

我是php的初学者。

anykind of help will be much appreciated.

任何帮助将不胜感激。

3 个解决方案

#1


0  

You will want to use strtotime and continue to use the $date variables:

您将需要使用strtotime并继续使用$ date变量:

// Setup the dates
list($d, $m, $Y) = explode('-', $date_str);
$date2 = Date('d-m-Y', mktime(0, 0, 0, $m, $d, $Y));
$date = Date($date2, mktime(0, 0, 0, $m, $d, $Y));

//Test de l\'existence du fichier
while ($i <= $periode) {
    $date = strtotime("+1 day", strtotime($date)); // 1 day past previous date
    $date2 = strtotime("+1 day", strtotime($date)); // 1 Day past the $date var
    echo date("Y-m-d", $date);

    var_dump($date);
    if (file_exists($rep . $ip . $date . '.txt')) {
        $var = 0;
    } else {
        fopen($rep . $ip . $date . '.txt', 'w+');
        $var = 1;
        //break 1;
    }
    $i++;
};
return $var;

#2


0  

try something along these lines.

尝试这些方面的东西。

$todaysdate = date("Y-m-d H:i:s");
$tomorrowsdate = date("Y-m-d H:i:s", date()+86400);

essentially i just added 86400 seconds to the current date, and there are 86400 seconds in a day, so I just added 1 day.

基本上我刚刚添加86400秒到当前日期,一天有86400秒,所以我只添加了1天。

#3


0  

Wouldn't it be easier for you to create date out of UNIX Timestamp and increment it? Like this:

您是否更容易从UNIX时间戳创建日期并增加它?像这样:

$time = time() + (3600 * 24);

$ time = time()+(3600 * 24);

$date = date('d-m-Y', $time);

$ date = date('d-m-Y',$ time);

#1


0  

You will want to use strtotime and continue to use the $date variables:

您将需要使用strtotime并继续使用$ date变量:

// Setup the dates
list($d, $m, $Y) = explode('-', $date_str);
$date2 = Date('d-m-Y', mktime(0, 0, 0, $m, $d, $Y));
$date = Date($date2, mktime(0, 0, 0, $m, $d, $Y));

//Test de l\'existence du fichier
while ($i <= $periode) {
    $date = strtotime("+1 day", strtotime($date)); // 1 day past previous date
    $date2 = strtotime("+1 day", strtotime($date)); // 1 Day past the $date var
    echo date("Y-m-d", $date);

    var_dump($date);
    if (file_exists($rep . $ip . $date . '.txt')) {
        $var = 0;
    } else {
        fopen($rep . $ip . $date . '.txt', 'w+');
        $var = 1;
        //break 1;
    }
    $i++;
};
return $var;

#2


0  

try something along these lines.

尝试这些方面的东西。

$todaysdate = date("Y-m-d H:i:s");
$tomorrowsdate = date("Y-m-d H:i:s", date()+86400);

essentially i just added 86400 seconds to the current date, and there are 86400 seconds in a day, so I just added 1 day.

基本上我刚刚添加86400秒到当前日期,一天有86400秒,所以我只添加了1天。

#3


0  

Wouldn't it be easier for you to create date out of UNIX Timestamp and increment it? Like this:

您是否更容易从UNIX时间戳创建日期并增加它?像这样:

$time = time() + (3600 * 24);

$ time = time()+(3600 * 24);

$date = date('d-m-Y', $time);

$ date = date('d-m-Y',$ time);