0

I'm looking for a way to automatically insert row to MySQL. Currently I using PHP Cron to do this, but values don't look perfect in MySQL - photo below.

enter image description here

What should I do to get clearly date_added values like for example:

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

I want to do this because it's important for me to make analytics and remove all empty data if exists. Below script is a 60 seconds cron made in php.

$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 Answers1

0

Change

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

into

$date = date('Y-m-d H:i:00');
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
  • Fine, but I don't know reason why Cron doing it not clearly? I mean is 2018-04-30 13:53:00 and doing it 2-3 minutes later. It's a server response problem or something with my script? –  Apr 30 '18 at 12:29
  • "Fine, but I don't know reason why Cron doing it not clearly?" I geuss a cronjob that's configured to run every minute might be executed between the 1 second and 59 second within that minute. – Raymond Nijland Apr 30 '18 at 12:34