使用PHP在60秒后自动将行插入MySQL

时间:2018-04-30 12:20:29

标签: php mysql cron

我正在寻找一种自动向MySQL插入行的方法。目前我使用PHP Cron来做到这一点,但是在MySQL中看起来并不完美 - 下面的照片。

enter image description here

如何清楚地获取date_added值,例如:

2018-04-30 13:50:00
2018-04-30 13:51:00
2018-04-30 13:52:00
2018-04-30 13:53:00
2018-04-30 13:54:00
2018-04-30 13:55:00
2018-04-30 13:56:00
2018-04-30 13:57:00

我想这样做,因为对我来说,如果存在分析并删除所有空数据,这对我很重要。下面的脚本是用PHP制作的60秒cron。

$date = date('Y-m-d H:i:s');

/* Define functions */
function write_cron($timestamp, $cron_name, $var_name) {
    $filename = realpath(dirname(__FILE__)).'/times/'.$cron_name.'.php';
    $content = file_put_contents($filename, '<? $'.$var_name.'[\'time\'] = \''.$timestamp.'\'; ?>');

    $return = true;
    if (!$content) {
        die('<center><b>System ERROR</b><br /><i>system/cron/times/'.$cron_name.'.php</i> must be writable (change permissions to 777)</center>');
        $return = false;
    }
    return $return;
}

/* Times */
$timestamp = time();
$daily_time = strtotime(date('j F Y'));

$realPath = realpath(dirname(__FILE__));
if (!is_writable($realPath.'/times')) {
    die('<center><b>System ERROR</b><br /><i>system/cron/times/</i> directory must be writable (change permissions to 777)</center>');
}

if (file_exists($realPath.'/times/5min_cron.php')) {
    include($realPath.'/times/5min_cron.php');
}

if ($cron_5min['time'] <= ($timestamp-60)) {
    $write = write_cron($timestamp, '5min_cron', 'cron_5min');
    if ($write) {
    $db->Query("INSERT INTO `m_z_analytics_empty` (`coins`, `1_min`, `1_day`, `date_added`) VALUES ('0.00', '1', '0', '".$date."')");
    }
}

5min_cron.php

<? $cron_5min['time'] = '1514768460'; ?>

1 个答案:

答案 0 :(得分:0)

更改

$date = date('Y-m-d H:i:s');

进入

$date = date('Y-m-d H:i:00');